I'm starting to use modules in my code and tried to create a simple exported function in a module using std::source_location. However, compilation fails at the function call stating error: ‘source_location’ is not a member of ‘std’.
This is the code in question:
util.cpp:
module;
#include <source_location>
#include <iostream>
export module util;
export using source_loc = std::source_location;
export void panic(const char* message, const source_loc loc = source_loc::current()) {
std::cout << "file: "
<< loc.file_name() << "(" << loc.line() << ":" << loc.column() << ") `"
<< loc.function_name() << "`: " << message << '\n';
exit(EXIT_FAILURE);
}
main.cpp:
import util;
int main() {
panic("oh noez"); // < compiler error here
}
The compiler gives a hint about forgetting to #include <source_location> and indeed if I add it to main.cpp it compiles and works as expected. Except I already exported std::source_location as source_loc from my util-module so I would expect it to be accessible in mainbecause of the import util statement. Having to explicitly #include <source_location> everywhere I use the defined panic function seems to defeat the very purpose of modules altogether.
What is going on here? Am I missing something essential about C++ modules? Or did I simply make a mistake that is easily fixed?
I'm using g++ 11.2 to compile the code like this: g++ -std=c++20 -fmodules-ts util.cpp main.cpp -o main