Home Uniform initialization on member initializer list error
Reply: 0

Uniform initialization on member initializer list error

user1857
1#
user1857 Published in May 20, 2018, 9:57 am

I am getting a compilation error on this C++11 code but I dont know why. This is the code:

#include <condition_variable>

class NonCopiableClass
{
    std::condition_variable condition_;
};

struct NonCopiableStruct
{
    std::condition_variable condition_;
};

class Test
{
 public:
    Test() : 
        myClass{},
        myStruct{}
    {};
 private:
    NonCopiableClass myClass;
    NonCopiableStruct myStruct;
};

Visual Studio 2015 fails with the following error:

error C2280: 'std::condition_variable::condition_variable(const std::condition_variable &)': attempting to reference a deleted function 1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\mutex(550): note: see declaration of 'std::condition_variable::condition_variable'.

If I change the Test constructor to not use C++11 uniform initialization of the Struct it compiles OK.

Test() : 
        myClass{},
        myStruct() // <--- CHANGE
    {};

I am not getting why for Struct type uses copy constructor but the Class seems OK. It only happends with Struct having non-copiable members.

I also noted that if I init the Struct outside of the Test Class member initializer list It works:

int main()
{
    NonCopiableStruct a{};   
    return 0;
}

Any idea Why is this code failing?. What is going on under the hood? What is the difference between myClass initialization and myStruct initialization?. Why it won't compile if used on the class member initializer list but is it okay I use it outside? I have tried on GCC and it seems to be okay.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303023 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO