Strange behavior of copy-initialization, doesn't call the copy-constructor!

前端 未结 6 671
时光说笑
时光说笑 2021-01-02 13:15

I was reading the difference between direct-initialization and copy-initialization (§8.5/12):

T x(a);  //direct-initialization
T y = a; //copy-initialization         


        
6条回答
  •  清酒与你
    2021-01-02 13:30

    In any compiler, syntax [and semantic] analysis process are done prior to the code optimization process.

    The code must be syntactically valid otherwise it won't even compile. Its only in the later phase (i.e code optimization) that the compiler decides to elide the temporary that it creates.

    So you need an accessible copy c-tor.

提交回复
热议问题