Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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?

like image 580
Christian Blume Avatar asked Apr 14 '18 11:04

Christian Blume


People also ask

What's new in C++17 in Visual Studio 2017?

For detailed information, see C++ Conformance Improvements in Visual Studio 2017. The compiler supports about 75% of the features that are new in C++17, including structured bindings, constexpr lambdas, if constexpr, inline variables, fold expressions, and adding noexcept to the type system. These features are available under the /std:c++17 option.

What are the compiler options available in Visual Studio 2017?

Visual Studio 2017 allows using /sdl with /await. We removed the /RTC limitation with Coroutines. /std:c++14 and /std:c++latest: These compiler options enable you to opt in to specific versions of the ISO C++ programming language in a project.

What version of Visual Studio 2017 is this warning on?

This warning is off by default in Visual Studio 2017 version 15.3, and only impacts code compiled with /Wall /WX. Starting in Visual Studio 2017 version 15.5, it's enabled by default as a level 3 warning.

What's new in MSVC in Visual Studio 2017?

With support for generalized constexpr and non-static data member initialization (NSDMI) for aggregates, the MSVC compiler in Visual Studio 2017 is now complete for features added in the C++14 standard. However, the compiler still lacks a few features from the C++11 and C++98 standards.


2 Answers

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;
}
like image 112
PolarBear Avatar answered Oct 20 '22 17:10

PolarBear


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;
}
like image 44
273K Avatar answered Oct 20 '22 18:10

273K