27

I read this link of Stroustrup with the following code:

class X {
        int a;
    public:
        X(int x) { if (0<x && x<=max) a=x; else throw bad_X(x); }
        X() :X{42} { }
        X(string s) :X{lexical_cast<int>(s)} { }
        // ...
    };

My question is about the line:

X() X{42}{}

Is there any differences between parentheses and curly brackets?
If there is no differences can I use curly brackets in other function calls as well? Or is it just in constructor delegation? And at last Why we should have both syntaxes? It is a little ambigous.

Community
  • 1
  • 1
Govan
  • 2,079
  • 4
  • 31
  • 53
  • 2
    "can I use curly brackets in othere functions calls as well or is it just in constructor delegation?" — Constructor delegation is not a function call. You can never use `{}` with function calls. – Emil Laine Oct 17 '15 at 10:37
  • check this : http://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives –  Oct 17 '15 at 10:44

2 Answers2

24

() uses value initialization if the parentheses are empty, or direct initialization if non-empty.

{} uses list initialization, which implies value initialization if the braces are empty, or aggregate initialization if the initialized object is an aggregate.

Since your X is a simple int, there's no difference between initializing it with () or {}.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • 1
    If I understand right it means if I am wrinting `X(): X(42){}` then I am calling the inte constructor but if I am writing the `X(): X{42}{}` then I am just initializing memebr variables without calling another constructor? – Govan Oct 17 '15 at 10:44
  • @Govan - primitive types such as `int` and `double` aren't full-fledged objects. They don't have constructors. – David Hammen Oct 17 '15 at 10:50
  • @DavidHammen I meant X(int constructor) not primitive int constructior – Govan Oct 17 '15 at 10:52
  • @Govan Since `int` has no constructor there is no constructor to be called while initializing `X`. Both `X(42)` and `X{42}` will perform the direct initialization, and since `X` is of type `int`, that means an implicit conversion from `42` to `int` will be performed to initialize `X`. So in the case of a simple `int`, both `()` and `{}` do the same thing. – Emil Laine Oct 17 '15 at 10:56
  • "Since X is of type `int`" - no it isn't! It's a fully fledged class. Admittedly it only has one data member of type `int`, but it has *three* user-defined constructors. – Martin Bonner supports Monica Jan 05 '18 at 13:39
6

Initialization values can be specified with parentheses or braces.

Braces initialization was introduced with C++11 and it is meant to be "uniform initialization" that can be used for all non-static variables.

Braces can be used in the place of parentheses or the equal sign and were introduced to increase uniformity and reduce confusion.

It is only a syntactical construct and does not result in performance benefits or penalties.

dspfnder
  • 1,135
  • 1
  • 8
  • 13