用空花括号初始化
Initialization with empty curly braces
第一次尝试,一切正常:
class Base {
public:
Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {};
另一种实现方式(添加explicit
):
class Base {
public:
explicit Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {}; // error! Why?
我在 cppreference 上读到,在这两种情况下都将使用默认初始化并且没有区别。
来自列表初始化:
Otherwise, If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.
从值初始化:
if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;
I have read on cppreference that in both cases default initialization would be used and no diffences.
不,它们不一样。准确地说,Base b{};
是direct-list-initialization, while Base b_one = {};
is copy-list-initialization;对于复制列表初始化,只能调用非explicit
构造函数。
(强调我的)
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)
第一次尝试,一切正常:
class Base {
public:
Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {};
另一种实现方式(添加explicit
):
class Base {
public:
explicit Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {}; // error! Why?
我在 cppreference 上读到,在这两种情况下都将使用默认初始化并且没有区别。
来自列表初始化:
Otherwise, If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.
从值初始化:
if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;
I have read on cppreference that in both cases default initialization would be used and no diffences.
不,它们不一样。准确地说,Base b{};
是direct-list-initialization, while Base b_one = {};
is copy-list-initialization;对于复制列表初始化,只能调用非explicit
构造函数。
(强调我的)
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)