Home > Mobile >  Error C2512 when creating a std::promise<std::tuple<T>> (Visual Studio-only)
Error C2512 when creating a std::promise<std::tuple<T>> (Visual Studio-only)

Time:09-23

I'm encountering an error (Visual Studio-only) when creating a std::promise<std::tuple<T>> whose type (T) has no default constructor.

I've created a derived example to highlight the issue.

#include <future>
#include <tuple>

class Foo {
public:
    Foo() = delete;

    Foo(int value) :
        mValue{ value } {

    }

private:
    int mValue;
};

int main()
{
    std::promise<std::tuple<Foo>> p{};
}
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>ConsoleApplication1.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\future(206): error C2512: 'std::tuple<Foo>::tuple': no appropriate default constructor available
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\future(203): message : while compiling class template member function 'std::_Associated_state<_Ty>::_Associated_state(std::_Deleter_base<_Ty> *)'
1>        with
1>        [
1>            _Ty=std::tuple<Foo>
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\future(1143): message : see reference to function template instantiation 'std::_Associated_state<_Ty>::_Associated_state(std::_Deleter_base<_Ty> *)' being compiled
1>        with
1>        [
1>            _Ty=std::tuple<Foo>
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\future(1143): message : see reference to class template instantiation 'std::_Associated_state<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=std::tuple<Foo>
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\future(1143): message : while compiling class template member function 'std::promise<std::tuple<Foo>>::promise(void)'
1>C:\Users\Quenton Jones\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp(22): message : see reference to function template instantiation 'std::promise<std::tuple<Foo>>::promise(void)' being compiled
1>C:\Users\Quenton Jones\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp(22): message : see reference to class template instantiation 'std::promise<std::tuple<Foo>>' being compiled
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

When compiling with clang, there's no issue.

Why is Visual Studio demanding that T have a default constructor?

CodePudding user response:

To workaround this known issue, I've done the following:

class Foo {
public:
#if _WIN32
    Foo() {
        throw std::exception("Invalid Constructor");
    }
#else
    Foo() = delete;
#endif

    Foo(int value) :
        mValue{ value } {

    }

private:
    int mValue;
};

CodePudding user response:

A common cause of error C2512 is when you define a class or struct constructor that takes arguments, and then you attempt to declare an instance of your class or struct without any arguments.For details, please refer to this document.

One more point, currently std::promise cannot be compiled with non-default constructible types.Using #if, #else is also a way.

  • Related