Up until now, I have been initializing most of my variables as such:

const QString foo("bar");

Which from my newfound understanding, is known as direct initialization. (http://en.cppreference.com/w/cpp/language/direct_initialization)

I was looking at someone's code, and in a namespace, they were initializing their variables with Curly Braces:

const QString foo{"bar"};

which also counts as direct initialization: enter image description here

I never knew this was a thing until today.

Code is more often read than written, what does direct initialization with Curly Braces imply as compared to Parentheses?

Just as I understand a const will tell me that a variable will not change, can I infer anything from this curly brace practice?

  • 9
    It implies the author is trying to adopt modern C++ practices. This is advocated, among others, by Scott Meyers in Effective C++ and I'm pretty sure I read it as well on Herb Sutter's blog. – Vincent Savard May 19 '17 at 12:49
  • 3
    This has already been answered on SO stackoverflow.com/questions/9976927/… – Peter M May 19 '17 at 12:52
  • @PeterM Doesn't that question also refer to list and aggregate initialization? I am asking specifically to direct initialization. – Akiva May 19 '17 at 13:06
  • 8
    "It implies the author is trying to adopt modern C++ practices.": This, however, does not say anything wrt why the new practice was introduced, e.g. what problems it tried to solve. In other words, I do not think "it is better because it is newer" is a sufficient argument to change coding style. – Giorgio May 19 '17 at 14:09
  • 4
    Wow, C++ has gotten weird in the last 12 years since I last looked at it... – FrustratedWithFormsDesigner May 19 '17 at 14:40

This shows that the author is using modern C++ (e.g. >= C++11) and applies good practice:

Braced initialization is the most widely usable initialization syntax, it prevents narrowing conversions and it's immune to C++'s most vexing parse.

-- Scott Meyers, in Effective Modern C++, Item 7

You have to be aware that there might however be a subtlety if a class has a constructor from an initializer list.

If you're not familiar with this syntax, I'd really recommend you to read Scott Meyer's above-mentioned book or acquire equivalent knowledge for example with Herb Sutter's videos, in order not to end up with outdated C++ skills.


I don't think you can infer much of anything. There are simply too many possibilities here to draw a meaningful inference.

First, keep in mind that all along, aggregates could be initialized using braces:

struct foo { 
    int x;
    long y;

// well formed since long before C++ existed:    
foo bar = { 1, 2 };

// New syntax
foo baz { 2, 3 };

So, the only difference in this case is leaving out the = between the name of the variable, and the initializer. That's kind of convenient, but doesn't really mean much. This could literally be a case of intending to use the old syntax, missing the = due to a typo, and never correcting/noticing it because the compiler didn't give an error message.

With that in mind, let's consider the cases where you once had to use parens instead of braces.

struct abc { 
    int x;
    abc(int x) : x(x) {}

So this, under C++ 98/03, we did initialization something like this:

abc def(1);

Now we can use:

abc def = {1};


abc def{1};

Again, I think it's entirely possible that in a fair number of cases, the abc def = {1}; case happens entirely by accident. Somebody wrote it without checking whether abc was an aggregate or not. At one time, the compiler would have complained and they'd have changed it to get it to compile. The compiler no longer complains, so they leave it alone.

Don't get me wrong: I'm all in favor of preventing narrowing conversions1. But, given human nature (and the amount of C++ I see written without knowing all the intimate details of the standard, not to mention all its most recent changes) I think there are probably a fair number of cases where things like this happen more or less by accident, and since they work they're left alone.

1. I'm a lot less excited about everything being interpreted as an initializer_list if at all possible though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.