3

The following code complains error C2512: 'MyStruct': no appropriate default constructor available when trying to compile with MSVC. It works fine with clang

struct MyStruct{
    int val;
    explicit MyStruct(int v): val(v){}
};

int main()
{
    auto ret = std::async([](){MyStruct ms{50}; return ms;});
    std::cout << ret.get().val;

    return 0;
}

Where does it require in this code to invoke the default constructor?

Saliya Ekanayake
  • 387
  • 3
  • 10
  • Does this solve your problem? [link](https://stackoverflow.com/questions/8689319/error-c2512-no-appropriate-default-constructor-available) – Alex May 20 '20 at 19:13
  • Maybe because MSVC is a buggy compiler? :) – Igor R. May 20 '20 at 19:15
  • I would speculate that MSVC's implementation of `std::future` maybe has a `T`/`MyStruct` member. Anyways, probably a MSVC bug – Sebastian Hoffmann May 20 '20 at 19:16
  • MyStruct ms{50}; - I think this invokes it. Why you use { } instead of ( ) ? – Alex May 20 '20 at 19:16
  • 2
    @Alex `{...}` is uniform initialization introduced in C++11 – Sebastian Hoffmann May 20 '20 at 19:17
  • 1
    This is not a problem with `MyStruct ms{50}`. It is the problem with returning ms and moving it into a **defualt constructed** resulting value holder. – Ali Tavakol May 20 '20 at 19:18
  • Hi, hope you don't mind that I marked this as duplicate of https://stackoverflow.com/questions/49830864 - I looked into it and both issues have the same underlying cause (std::async uses the std::future/std::promise machinery internally). – ecatmur Jul 28 '20 at 08:45

0 Answers0