0

I recently caught up on Member Initializer Lists.
The Cpp-Learning-Site suggests to always prefer direct list initialization over direct initilization.

int a(5);
int b{5};    // preferred

I noticed that both of these can be used in Member initializer lists.

Which one of the following is better and for which reasons?
Thank you in advance.

class Foo {
public:
    int a;
    Foo(int a): a(a) {}
};

class Bar {
public:
    int a;
    Bar(int a): a{a} {}
};
Raumschifffan
  • 360
  • 2
  • 13
  • 1
    I usually use the list initialization to disallow narrowing. Also, if you use list initialization you wont run in to the [most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse). Of course, you can't always use list initialization, like for example if you're trying to call one of `std::vector`'s overloaded constructors. – WBuck Aug 04 '21 at 00:20
  • [Good discussion (among the other wisdom found in the Core Guidelines)](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es23-prefer-the--initializer-syntax) – user4581301 Aug 04 '21 at 01:03

0 Answers0