This question would an "update" about this kind of topics, like this old question (Is it possible to declare constexpr class in a header and define it in a separate .cpp file?), but now with C++ modules, in the context about functions that are called from multiple files and not only called from the same file.
About functions that with a traditional file structure, .h/header and .cpp files, need to be placed entirely in the header file:
constexpr functionstemplate functionsNow with C++ modules, are there changes about it?
Is it possible to split declaration and definition in 2 files?
As example, for now and about a constexpr function, after doing some tests I notice it can be split but into the same .cppm module file, not in 2 separate files:
// A.cppm
export module A;
export constexpr int sum(int a, int b);
// ...
constexpr int sum(int a, int b)
{
return a + b;
}
// main.cpp
import A;
import std;
int main()
{
// Call without `constexpr`
int w = sum(1, 2);
// Call with `constexpr`
constexpr int a = 3;
constexpr int b = 4;
constexpr int z = sum(a, b);
std::cout << "w: " << w << "n";
std::cout << "z: " << z << "n";
}
برچسب:
نویسنده: استخدام کار