Search code examples
c++c++11constructorinitializationexplicit

Initialization with empty curly braces


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;


Solution

  • 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)