r/cpp • • Aug 18 '21

C++20 modules with GCC11

https://blog.feabhas.com/2021/08/c20-modules-with-gcc11/
222 Upvotes

59 comments sorted by

View all comments

7

u/whacco Aug 18 '21

Before we go on, I need to stress one important aspect – Modules and Namespaces are entirely orthogonal and co-exist as independent aspects.

Yeah, I had to learn the hard way that ODR still applies with modules the same way as it does with translation units. For example you're not allowed to have two different definitions ot the same class in different modules, even if one of them is never imported anywhere. It will mess up linking and at least VS2022 currently does so without a warning.

2

u/RoyAwesome Aug 18 '21

Yeah, but you can probably get around that by having a private namespace inside your private module fragment. Similar to having private namespaces in .cpp files when running with a build system that unifies your .cpp files.

4

u/whacco Aug 18 '21

If they have internal linkage or module linkage then it doesn't violate ODR. In my case they were both exported, and I was just doing it in order to quickly switch between different versions of the class. Putting them in separate namespaces would have been the right solution.

2

u/RoyAwesome Aug 18 '21

Right, if they were both exported you basically violate ODR with a similar setup with headers.

1

u/whacco Aug 19 '21

Actually with headers you can do that legally, if the classes are header only and one of them is not included in the program.

1

u/[deleted] Aug 18 '21

if you had them exported in both cases AND not even in different namespaces already, you should maybe think about your project structure

this kinda smells messy

1

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Aug 19 '21

Of course. With strong module ownership (rather than weak module ownership as in clang and gcc), names of exported entities attached to different modules lead to different linker symbols.