Initialization with empty curly braces

一笑奈何 提交于 2020-05-11 07:32:07

问题


First attempt and everything works fine:

class Base {
 public:
    Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {};

Another way of implementation(add explicit):

class Base {
 public:
    explicit Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {};  // error! Why?

I have read on cppreference that in both cases default initialization would be used and no diffences.

From list initialization:

Otherwise, If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.

From value initialization:

if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;


回答1:


I have read on cppreference that in both cases default initialization would be used and no diffences.

No they're not the same. To be precise, Base b{}; is direct-list-initialization, while Base b_one = {}; is copy-list-initialization; for copy-list-initialization, only non-explicit constructor may be called.

(emphasis mine)

  • direct-list-initialization (both explicit and non-explicit constructors are considered)

  • copy-list-initialization (both explicit and non-explicit constructors are considered, but only non-explicit constructors may be called)



来源:https://stackoverflow.com/questions/51862128/initialization-with-empty-curly-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!