Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Both including and importing std

I have a C++ module that imports std:

// MyModule.ixx
export module MyModule;

import std;

export namespace MyNamespace {
   class MyClass : std::monostate {};
}

I want to import the module in another file:

// main.cpp
#include <QtCore/QDebug>

import MyModule;

auto main(int argc, char **argv) -> int {
   qDebug() << "test";
   MyNamespace::MyClass A;
}

This gives a compilation error:

main.cpp(7,25): fatal  error C1117: unrecoverable error importing module 'std': symbol 'monostate' has already been defined

If I remove #include <QtCore/QDebug> and qDebug() << "test";, the program compiles fine. So it seems that #include <QtCore/QDebug> indirectly includes std::monostate, and that it conflicts with the one imported by import std; in MyModule.ixx.

How can I resolve this? Can't I import std when somewhere else in my program the standard library is #included?

like image 432
Magnar Myrtveit Avatar asked Jun 04 '26 04:06

Magnar Myrtveit


1 Answers

I solved it by importing <variant> instead of std:

// MyModule.ixx
export module MyModule;

import <variant>;

export namespace MyNamespace {
   class MyClass : std::monostate {};
}
like image 138
Magnar Myrtveit Avatar answered Jun 07 '26 22:06

Magnar Myrtveit