0

First of all, I've found the term "initializer list" being used for two seemingly different things: Initializing classes using curly braces like this Foo foo{1} and writing constructors using this syntax Foo::Foo(int test) : m_test(test). Are these two connected or am I just mixing things up?

Now to my actual question. I've read that the curly brace initialization does not allow narrowing. However, that can't be the only difference.

struct Test {
    int one;
    int two;
};

int main() {
    Test test{1, 2};
}

This code works, even though Test::Test(int,int) is declared nowhere. So what exactly does the curly brace initialization do differently from normal initialization? Is it just syntactic sugar or are there deeper reasons as to why it exists?

Jan Berndt
  • 913
  • 1
  • 10
  • 22
  • 1
    *"Are these two connected"* Not really. The first one is a "braced-init-list", the second one is "member initializer list". – HolyBlackCat Mar 16 '20 at 17:52
  • related/dupe: https://stackoverflow.com/questions/46569038/list-initialization-initializer-list-and-related-questions-in-c – NathanOliver Mar 16 '20 at 17:53
  • This is [aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization). – Marek R Mar 16 '20 at 17:55

0 Answers0