r/cpp • MSVC Game Dev PM • Sep 14 '20

Standard C++20 Modules support with MSVC in Visual Studio 2019 version 16.8 | C++ Team Blog

https://devblogs.microsoft.com/cppblog/standard-c20-modules-support-with-msvc-in-visual-studio-2019-version-16-8/
254 Upvotes

128 comments sorted by

View all comments

Show parent comments

1

u/atimholt Sep 15 '20

—Though I sure wouldn't mind something that allows you to choose/“whitelist” what symbols you're importing, similar to Python's “import [library] as [whatever identifier you want]”, which combines import with using while allowing post hoc control of what you actually want to enter your translation unit.

That said, the need for such a facility is incredibly small. As much as it's possible to get collision between top-level namespace names, I've never once heard anyone actually complain about it. I guess if it did happen to you, you could wrap it in something like:

// lib_wrapper.cppm
export module lib_wrapper;

import colliding_library;
export namespace wrapped_lib = colliding_library_namespace;

/* It *might* make sense to name the namespace differently from
 * the file, just to emphasize that this is only a "waypoint"
 * for the imported symbols, not their source.
 * 
 * You'd want the relation between the two to remain obvious
 * at least, of course.
 */

// EOF

4

u/MonokelPinguin Sep 15 '20

For such a thing to work, you basically need the strong ownership model. This is not about what symbols you import, but if the symbol causes linker conflicts, when it is defined in more than one library. With the weak model, this always causes linker issues. With the strong model it only causes issues, when you import both symbols in the same unit.

2

u/atimholt Sep 15 '20

Indeed. I'm not a fan of half-efforts that actually make things impossible, however rarely the need arises.

1

u/Plorkyeran Sep 15 '20

It's definitely a niche problem, but I have had to do dumb things with linkers to deal with symbol name collisions before.