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?