Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<thread>: Using this_thread::sleep_for in a module results in compilation errors #5203

Open
hexne opened this issue Dec 23, 2024 · 2 comments
Labels
compiler Compiler work involved modules C++23 modules, C++20 header units

Comments

@hexne
Copy link

hexne commented Dec 23, 2024

When I use vs and use std::this_thread::sleep_for in a C++ module, I get an error.

'std::this_thread::sleep_until': no matching overloaded function found
binary '+': 'const std::chrono::time_point<std::chrono::steady_clock,std::chrono::duration<__int64,std::ratio<1,1000000000>>>' does not define this operator or a conversion to a type acceptable to the predefined operator
binary '+=': no global operator found which takes type 'const std::chrono::duration<__int64,std::ratio<1,1000>>' (or there is no acceptable conversion)
binary '-': 'const std::chrono::time_point<std::chrono::steady_clock,std::chrono::duration<__int64,std::ratio<1,1000000000>>>' does not define this operator or a conversion to a type acceptable to the predefined operator
binary '>': 'const std::chrono::duration<__int64,std::ratio<1,1000>>' does not define this operator or a conversion to a type acceptable to the predefined operator

The minimum duplicate file is as follows:
Mouse.ixx

module;
#include <thread>
#include <chrono>

export module Mouse;

export struct Mouse {
    Mouse() {
        std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
    }
};

main.cpp

import Mouse;

int main() {
	Mouse mouse;

	return 0;
}

A possible solution:
Modify #include <thread> and #include <chrono> in Mouse.ixx to import std to pass the compilation. The modified Mouse.ixx file is as follows:

export module Mouse;
import std;
export struct Mouse {
    Mouse() {
        std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
    }
};

The main.cpp file does not need to be modified and can now be compiled.
I am using the vs version:

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.12.3
@oliverportcnc
Copy link

oliverportcnc commented Jan 6, 2025

Interestingly, I've found when I've run into these types of issues MSVC always compiles if the code is extracted to a definition outside the class definition:

export struct Mouse {
    Mouse();
};

Mouse::Mouse() {
    std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
}

Haven't tested this example though. I've encountered the same chrono/sleep bug in our codebase.

@code-npc
Copy link

code-npc commented Jan 7, 2025

Interestingly, I've found when I've run into these types of issues MSVC always compiles if the code is extracted to a definition outside the class definition:

export struct Mouse {
    Mouse();
};

Mouse::Mouse() {
    std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
}

Haven't tested this example though. I've encountered the same chrono/sleep bug in our codebase.

As you said, I tested this example and it compiled correctly when the member function was defined outside the class definition,The complete code is as follows:
Mouse.ixx

module;
#include <chrono>
#include <thread>
export module Mouse;

export struct Mouse {
    Mouse();
};

Mouse::Mouse() {
    std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
}

@StephanTLavavej StephanTLavavej added compiler Compiler work involved modules C++23 modules, C++20 header units labels Jan 8, 2025
@StephanTLavavej StephanTLavavej changed the title <thread>: Using std::this_thread::sleep_for in a module results in compilation errors <thread>: Using this_thread::sleep_for in a module results in compilation errors Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Compiler work involved modules C++23 modules, C++20 header units
Projects
None yet
Development

No branches or pull requests

4 participants