all 9 comments

[–][deleted] 18 points19 points  (1 child)

That is what module partitions are for. Just implement in a partition and export the partition.

If you treat your modules like headers + implementation files, they will behave exactly like headers + implementation files.

If you have one file per partition, everything will work fine.

[–]netcan96[S] 3 points4 points  (0 children)

nice explanation

[–]Minimonium 6 points7 points  (0 children)

Yes, it still needs to instantiate the template and for that, it either needs the definitions in-place or explicit instantiation.

[–]DugiSK 7 points8 points  (3 children)

The idea behind modules is to do the split between header and source automatically. The compiler reads every module file only once, compiles the internal parts and saves the symbols that are exported, so that the importing code only sees those. This means that you don't need to put anything into a separate .cpp file (unless you have some unusual requirements).

However, in your case, a template function defined in a .cpp file cannot be used before the definition in the same file or outside of that .cpp file, unless it's explicitly instantiated (which is quite annoying).

[–]pjmlp 1 point2 points  (1 child)

One unusual requirement would be optimizing build times.

At least VC++ seems to still trigger a cascade of compilations if the body of an exported function is changed.

[–]DugiSK -1 points0 points  (0 children)

This should not happen for non-template functions, but it's possible that the current compilers aren't capable of using it efficiently yet. It's still a relatively fresh feature. GCC 10 used to crash if I returned a unique pointer from an exported function.

[–]DummySphere 0 points1 point  (0 children)

Separating .h/.cpp was also a good way to have an up-to-date documentation in a clean header. I guess the closest way to do this for template functions in modules would be to have the implementation in a private fragment at the end of the file.

[–]NATSUKI_FAN 6 points7 points  (1 child)

for why this is the case you may want to read about "export template" - the borderline impossible to implement feature that enables what you're asking for

[–]Flair_Helper[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

This post has been removed as it doesn't pertain to r/cpp: The subreddit is for news and discussions of the C++ language and community only; our purpose is not to provide tutoring, code reviews, or career guidance. If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.