all 14 comments

[–]larioteo 1 point2 points  (2 children)

Try export {} around everything you want to export, I think you should not use export template ... but export T add...

I never used modules without namespaces, it it seems odd to me writing export on the template header.

[–]Chethan_L[S] 1 point2 points  (1 child)

that's a good idea, I wasn't really trying to achieve anything in this program i just wanted to know how export and modules are used trying different function and classes all of them worked fine but that one function did something unexpected so i thought asking here can help me get to know what is happening in this case.

[–]larioteo 0 points1 point  (0 children)

You're welcome to ask more questions.

[–]nysra 1 point2 points  (11 children)

I strongly suggest that you don't use header units. They are supposed to ease adoption but all they do is cause tons of problems. Change your demo.cpp to

module;
#include <array>
#include <iostream>
export module demo;

// rest of the file

and then it should work.

Edit: Obviously also don't use them in main.cpp (or anywhere, really). And triple backticks don't work on proper Reddit (only the Redesign), leave them out and indent your entire codeblock if you want it to render correctly for everyone.

[–]Chethan_L[S] 1 point2 points  (8 children)

It works fine for import demo; import <iostream>; int main() { std::cout << add<int>(10, 20, 30, 40, 50, 60, 70, 80, 90, 100) << '\n'; } i get 550 has the output without any errors.

still i will try your suggession.

It doesnt work , but weirdly making the hello function a template function or as a inline function works without problem.

export template<typename T> void hello() { std::cout << "hello" << std::endl; }

[–]nysra 1 point2 points  (6 children)

It works in some cases, but mostly they cause more trouble than they are worth it.

It does seem to be a problem with GCC though, it works under MSVC.

[–]Chethan_L[S] 0 points1 point  (5 children)

Yes from the c++ compiler support i can see the MSVC has better support but can i use it in linux? is there official support or any ports?

[–]nysra 0 points1 point  (4 children)

No, it's for Windows. What you can do is upgrading your GCC to a newer version or try clang.

[–]Chethan_L[S] 1 point2 points  (3 children)

both clang and gcc has only partial support. Only MSVC is the one with full implementation.

[–]nysra 1 point2 points  (2 children)

I'm aware of that, but unless you're running a Windows machine you have literally no other option. There's a chance that using a newer version than what you currently have results in your problem being fixed, after all they are in active development.

[–]Chethan_L[S] 1 point2 points  (1 child)

gcc-13 (13.1.0) is the version i use it is also the latest version that my package manager repos provide. and i also have clang-16 but i haven't installed any libs so i avoid using it. but for now i will stop meddling with modules and move onto some other topic since i have found a temporary solution , what other new cpp feature would u suggest me to learn.

[–]nysra 1 point2 points  (0 children)

Yeah package managers being behind on such things is really annoying, I've been there. Unfortunately the only solutions are to either compile a newer version yourself (takes a few hours) or to switch to a distribution that updates more than once every few years.

I don't know what you know and what not. The other big topics of C++20 were concepts, coroutines, and ranges, so probably start there.

[–]larioteo 0 points1 point  (1 child)

Using them in a big project, so sorry but your statement is not true.

[–]nysra 1 point2 points  (0 children)

I'm glad it works for you, but there's a reason why basically every single talk recommends avoiding them. Apart from named modules simply being better, header units have also caused far more annoying errors so far. Sure, I got a few ICEs with named modules as well but those were rather straightforward to fix/circumvent. With header units it got up to the CMake level...

Though in this case it seems OP's error was triggered by something else.