What's the motivation behind having copy and direct initialization behave differently?

前端 未结 4 1756
青春惊慌失措
青春惊慌失措 2020-11-30 02:20

Somewhat related to Why is copy constructor called instead of conversion constructor?

There are two syntaxes for initialization, direct- and copy-initialization:

4条回答
  •  时光说笑
    2020-11-30 02:46

    Only a speculation, but I am afraid it will be hard to be more certain without Bjarne Stroustrup confirming how it really was:

    It was designed this way because it was assumed such behaviour will be expected by the programmer, that he will expect the copy to be done when = sign is used, and not done with the direct initializer syntax.

    I think the possible copy elision was only added in later versions of the standard, but I am not sure - this is something somebody may be able to tell certainly by checking the standard history.

提交回复
热议问题