Most vexing parse C++11

后端 未结 2 1303
刺人心
刺人心 2020-12-22 02:20

I\'m confused about Y y {X{}}; what exactly this line does and what is its connection to the most vexing parse. A brief explanation is appreciated:



        
2条回答
  •  死守一世寂寞
    2020-12-22 02:53

    Y y {X{}};
    

    This is perfect and creates an object y passing a temporary object of type X to the constructor. There is NO vexing parse (most or otherwise). In fact, the construction using {} was introduced to solve the issue of vexing parse in many cases, such as these:

     Y y1();
     Y y2(X());
    

    Both belongs to (most) vexing parse because of which both declares functions, instead of objects.

    However, if you use curly braces called brace-initialization:

     Y y1{};
     Y y2{X{}}; //as you've done yourself
    

    then both declares objects, not functions, as expected.

提交回复
热议问题