I came across a strange problem. I made a C++20 module with the following content (file module.cppm):
module;
#include <regex>
#include <string>
export module foo;
export namespace bar {
using std::regex;
using std::smatch;
using std::regex_search;
using std::string;
}
I use this module in the following code (file main.cpp):
import foo;
int main() {
bar::regex regex;
bar::smatch match;
bar::string s;
bar::regex_search(s, match, regex);
}
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 4.0.0)
set(CMAKE_CXX_STANDARD 23)
project(cpptest)
add_library(module STATIC)
target_sources(module PUBLIC FILE_SET CXX_MODULES FILES module.cppm)
add_executable(cpptest main.cpp)
target_link_libraries(cpptest module)
When trying to build with the following commands:
cmake -B build -G Ninja
cmake --build ./build/
I get the following errors:
/usr/local/gcc-15.1/include/c++/15/bits/regex.tcc:56:16: error: ‘std::__cxx11::basic_regex<char>::_AutomatonPtr std::__cxx11::basic_regex<char>::_M_automaton’ is private within this context
56 | if (__re._M_automaton == nullptr)
| ~~~~~^~~~~~~~~~~~
/usr/local/gcc-15.1/include/c++/15/bits/regex.h:848:25: note: declared private here
848 | _AutomatonPtr _M_automaton;
My compiler is GCC 15.1, cmake 4.0.3. Here is a reproduction of this example on godbolt. If you compile the same example in Clang, there is no problem.
Is there a quick fix for this? Is GCC aware of this issue?