I am currently using Visual Studio 2022 Update 17.1.6, and I found something interesting with exporting type alias. For reasons I don't understand, when I export a type alias for some data type such as std::vector<std::string>
in a module interface file, I can use both std::vector<>
and std::string
in the file that imported it. For example:
modInterface.ixx
export module words;
import <iostream>
import <vector>;
import <string>;
...
export using Words = std::vector<std::string>;
...
In an internal partition:
modInternalPartition.cpp
module words:wordsIP;
import words;
//This compiles as expected
Words wordStorage;
//Why does my compiler sees below as correct, and compiles it without error?
std::vector<int> numStorage = { 1, 2, 3, 4 };
//Why does my compiler also sees below as correct, and compiles it without error?
std::string text = "This dish is tasty";
//This would produce an error, which is expected since I did not export import <iostream> in modInterface.ixx
std::cout << text;
...
My first thought was that since Words
is a type alias, exporting it would mean exporting std::vector<>
and std::string
, but since std::vector<>
is a template, why is it not the case that only the instantiation of it (std::vector<std::string>
) is exported?
std::vector<T>
without also exportingT
, because otherwise how are a bunch of member functions supposed to work? But I don't know the actual standard language to that effect, or whether or not you're supposed to get the whole template. Interesting question. – QuiritesT
would need to be exported given the nature of the type alias after some own thinking. I'm wondering why C++20 (or just Visual C++20) decided that I can just use all possible instantiations ofstd::vector<T>
rather than the specific instance I used, since even if I just have that specific instance, I would likely still be able to access all ofstd::vector<T>
's member functions. – Mephistophelesimport
andexport module
? new c++ keywords? – Pembrookimport
is analogous to#include
, whileexport module
means that the module file can be imported to other files, even those that are not in the same module. Bit of a simplification though. – Mephistopheles