What are the type requirements for `std::future` in C++20?

133 views Asked by At

cppreference doesn't list any type requirements for std::future. But when I try to compile this code with Visual Studio 2019:

#include <future>

struct A {
    A(int&){}

    A(A&&) = default;
    A(A const&) = delete;

    A& operator=(A&&) = delete;
    A& operator=(A const&) = delete;
};

int main(){
    int v;
    std::async([&v]{
        return A(v);
    }).get();
}

I get the error:

example.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\future(314): error C2280: 'A &A::operator =(A &&)': attempting to reference a deleted function
<source>(9): note: see declaration of 'A::operator ='
<source>(9): note: 'A &A::operator =(A &&)': function was explicitly deleted
C:/data/msvc/14.29.30151/include\future(309): note: while compiling class template member function 'void std::_Associated_state<_Ty>::_Set_value_raw(_Ty &&,std::unique_lock<std::mutex> *,bool)'
        with
        [
            _Ty=A
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\future(305): note: see reference to function template instantiation 'void std::_Associated_state<_Ty>::_Set_value_raw(_Ty &&,std::unique_lock<std::mutex> *,bool)' being compiled
        with
        [
            _Ty=A
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\future(774): note: see reference to class template instantiation 'std::_Associated_state<_Ty>' being compiled
        with
        [
            _Ty=A
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\future(769): note: while compiling class template member function '_Ty &std::_State_manager<_Ty>::_Get_value(void) const'
        with
        [
            _Ty=A
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\future(884): note: see reference to function template instantiation '_Ty &std::_State_manager<_Ty>::_Get_value(void) const' being compiled
        with
        [
            _Ty=A
        ]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\future(860): note: see reference to class template instantiation 'std::_State_manager<_Ty>' being compiled
        with
        [
            _Ty=A
        ]
<source>(17): note: see reference to class template instantiation 'std::future<A>' being compiled
Compiler returned: 2

It seams to require eigther a default constructor or a move assign operator.

With libstdc++ on GCC and clang it compiles. I didn't test with libc++. Are there any type requirements for std::future or is the standard library of MSVC 2019 just implemented incorrectly?

0

There are 0 answers