简体   繁体   中英

std::promise<T> where T must be default constructible in Visual Studio 2017?

I am trying to compile the following code in Visual Studio 2017:

#include <future>

int main()
{
    std::promise<std::reference_wrapper<int>> promise;
    (void)promise;
}

However, I get the following error:

error C2512: 'std::reference_wrapper': no appropriate default constructor available

Whereas it compiles fine with GCC and Clang.

Is this is a definite bug in Visual Studio or is it a valid implementation of std::promise?

Looks like it is a known issue in MSVC's standard library implementation. A simpler reproduction scenario:

#include <future>
struct NoDefaultCtor
{
    NoDefaultCtor() = delete;
};
int main() {
    std::promise<NoDefaultCtor> p;
    return 0;
}

I suppose you do not need std::reference_wrapper<int> . There is the suitable overloaded template for std::promise available:

template<class R> class promise<R&>;

Therefore you can fix your code in Visual Studio 2017:

#include <future>

int main()
{
    std::promise<int&> promise;
    (void)promise;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM