In C++, the concepts of uniform initialization and list initialization are closely related but represent different aspects of the brace ({}) initialization syntax introduced in C++11. Let's break down these concepts to clarify how they apply to your code:
Uniform Initialization
Uniform initialization is a broad term introduced in C++11 that refers to the use of curly braces {} for initializing objects. The primary goal of uniform initialization is to provide a consistent syntax for initializing objects of any type, whether they are built-in types, aggregates, or user-defined types. This syntax helps to avoid certain ambiguities and pitfalls associated with traditional parentheses () or assignment = initializations.
Key Points:
- Syntax: Uses curly braces
{}.
- Purpose: Provides a unified and consistent way to initialize different types of objects.
- Types of Uniform Initialization:
- Direct-list-initialization: e.g.,
Foo foo{1, 2};
- Copy-list-initialization: e.g.,
Foo foo = {1, 2};
List Initialization
List initialization is a specific type of uniform initialization that involves initializing an object using an initializer list. It has particular rules that dictate how constructors are selected, especially when std::initializer_list constructors are present.
Key Points:
- Overload Resolution: When a class has constructors that take an
std::initializer_list, these constructors are preferred during list initialization.
- Narrowing Conversions: List initialization prohibits narrowing conversions, which can help prevent certain types of bugs.
- Brace Elision: In some cases, braces can be omitted for nested initializations, but the presence of braces signals list initialization.
Applying to Your Code
Given your code:
#include <iostream>
#include <initializer_list>
using namespace std;
struct Foo {
Foo(int a, int b) { cout << 1; }
Foo(initializer_list<int> il) { cout << 2; }
};
int main() {
Foo foo{1, 2}; // Outputs: 2
}
Foo foo{1, 2}; uses uniform initialization because it employs the curly brace syntax to initialize foo.
- Additionally, because
Foo has a constructor that takes an std::initializer_list<int>, and the brace syntax matches this constructor's signature, list initialization rules apply.
- Overload Resolution: The presence of the
initializer_list constructor takes precedence, causing the Foo(initializer_list<int> il) constructor to be invoked, which outputs 2.
Conclusion
Foo foo{1, 2}; is an example of uniform initialization because it uses the brace {} syntax introduced to provide a consistent initialization method across different types.
- Simultaneously, it's also an instance of list initialization because it leverages an
std::initializer_list constructor, and the initializer list rules determine which constructor is called.
Thus, Foo foo{1, 2}; should be considered both uniform initialization and list initialization. The uniform aspect refers to the syntax and general initialization mechanism, while the list aspect pertains to how the initializer list interacts with constructor selection.
Additional Resources
Yes; brace-based initialization like Foo foo{1, 2} is a form of uniform initialization and specifically uses list initialization, so it is considered both uniform and list initialization.