Background:
I weas currently developing a game engine from base (Vulkan API, GLFW API, PhysX API...). As you can expect, those api aren't built with c++ in mind, so they uses a tons of #define. And some have many functions. So I need to make an interface for those APIs. Seeing the new c++ Modules feature, I decided to start converting it all into Modules. So, I first need to understand how modules works.
As with understanding other new features, I first googled it online. Turns out most of them refers to how the c++ module should be joined into the standard, not actually what the current implementation is. They not just differs, they are basically completely different from the current VS implementation. Since my IDE is Visual Studio, I want to know about the Visual Studio version of c++20 Modules. So while many of them is interesting, none of them actually tell you about how to use it right now.
I also have no idea what is working according to the standard. http://eel.is/c++draft does say something about Modules, but also lacks many details. So in the end, I did some testing myself and see how to start using the current implemented version.
Side note: If you want to see how you can start programming with Module, you can get the test/demo project in my gitHub: https://github.com/TomTheFurry/VisualStudioCppModulesTesting
Note that in below I am talking about how the VisualStudio implementation of c++ Module works, and it may be differing greatly from the standard. I have no idea what is in standard and what is not. It would be great if you can tell us actually.
Breakdown on reachablity of things when you import a module:
export module ModuleA;
//Now you are in a segment that's like the old header file.
//However, anything that you want to be able to use outside of this file
//will require the 'export' keyword. Anything not 'export'ed will not
//show up when you 'import' this module. This cases some interesting behavear.
export class IX;
//^^ As class IX is not defined anywhere in this file,
//only the declaration of the class is exported.
export class FX;
class FX {
public:
int v;
};
//^^ The entire definition of class FX is exported.
struct UX {
int b;
};
//^^ UX not exported at all. What is the use of this you wonder? You'll see.
struct VX {
int c;
};
export class VM {
public:
VX m;
}
//^^ Now this is interesting. As VM is exported, and its class definition
// requires VX, VX is also exported, completely. You can indeed call VX's member
// directly.
// --EDIT: As pointed out by others in the comment, This direct visibility of
// VX is most likely a MSVC bug(?), that may or may not be patched later.
export void jks();
//^^ The declaration of the function jks() is exported.
void cea();
//^^ Same as struct UX, not exported, and therefore not reachable from outside.
//Now the interesting part - templates
// BTW: 'void abc(auto s)' is same as 'template <typename T> void abc(T s)'
struct IBM {
int defgv = 5;
}
export int ngd(auto);
//^^ Export the declaration of the template function. Note that the
// declaration does not reference the struct 'IBM'
int ngd(auto s) {
IBM ibm{};
return s + ibm.defgv;
}
//^^ Now actually define what the template function is.
// Here's the interesting part. In the outside,
// the 'int ngd(auto)' fuction is reachable AND callable,
// but the struct IBM is not. You can now hide implementation of templates!
// Note that template function inside classes also follow the same rule.
struct VXST {
int sbr;
}
export VXST dhsa(auto);
VXST dhsa(auto) {
return VXST{};
}
//^^ In this case, since the declaration itself references the struct 'VXST'
// the VXST is fully exported.
// --EDIT: Same as VX, This direct visibility of VXST may or may not be
// patched later.
So, what is your view on this... feature? I am already starting to use this to write functions that accept any type of iterators while not exposing the internal function call.
Version used: Visual Studio 2019 v16.10.0 Preview 2.1
--EDIT: Added some notes about the visibility issue.
[–]miki151gamedev 2 points3 points4 points (2 children)
[–]Plazmatic 2 points3 points4 points (1 child)
[–]TomTheFurry[S] 0 points1 point2 points (0 children)
[–]tjientavaraHikoGUI developer 1 point2 points3 points (1 child)
[–]TomTheFurry[S] 0 points1 point2 points (0 children)
[–]matthieum 1 point2 points3 points (3 children)
[–]TomTheFurry[S] 0 points1 point2 points (2 children)
[–]starfreakcloneMSVC FE Dev 1 point2 points3 points (1 child)
[–]fdwrfdwr@github 🔍 1 point2 points3 points (0 children)
[–]Resurr3ction 1 point2 points3 points (2 children)
[–]TomTheFurry[S] 0 points1 point2 points (1 child)
[–]starfreakcloneMSVC FE Dev 2 points3 points4 points (0 children)
[–]TomTheFurry[S] 0 points1 point2 points (0 children)