What is uniform initialization in C++?

The lesson recommends using uniform initialization with braces (like int x {5};) instead of the = operator. What advantages does this have?

Uniform initialization using braces (e.g., int x {5};) was introduced in C++11 as a way to standardize initialization across different types. It has a few advantages over using the assignment operator =:

It prevents narrowing conversions. If you try to initialize a variable with a value that would require narrowing (like initializing an integer with a floating-point value), the compiler will throw an error:

int x = 5.5; // Compiles, but loses data 
int y {5.5}; // Fails to compile

It can be used to initialize any type, including user-defined types. This makes initialization syntax more consistent across types.

Uniform initialization can be used to initialize containers and complex types in a cleaner way, especially when the initialization involves multiple values:

#include <vector>

int main() {
  // Old style
  std::vector<int> v = {1, 2, 3};

  // Uniform initialization
  std::vector<int> v{1, 2, 3};
}

Additionally, uniform initialization avoids the "most vexing parse" problem in certain situations. In the following example, the first line is ambiguous. It could be a variable x initialized with a temporary MyClass.

But C++ allows superfluous parentheses around function parameter declarations, so x could be declaring a function that returns MyClass and takes a MyClass parameter.

The second line is unambiguously a variable.

class MyClass {};

// Is this a variable or a function declaration?
MyClass x(MyClass());

// Clearly a variable
MyClass y{MyClass()};

However, it's worth noting that uniform initialization isn't a complete replacement for the = syntax. In some cases, like with auto type deduction, the two forms can behave slightly differently.

Variables, Types and Operators

Learn the fundamentals of C++ programming: declaring variables, using built-in data types, and performing operations with operators

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

How does integer division work in C++?
Can you explain more about how integer division and the modulus operator work in C++, with some examples?
Implicit casts between booleans and numbers in C++
The lesson mentions that numeric types can be implicitly cast to booleans in C++, with 0 being false and anything else true. Can you clarify how that works with an example?
When should I use auto in C++?
The auto keyword seems convenient, but the lesson says to use it sparingly. When is it appropriate to use auto in C++?
How does operator precedence work in C++?
Can you clarify the rules around operator precedence in C++? How can I control the order of operations?
Prefix vs postfix increment in C++
What's the difference between the prefix and postfix increment/decrement operators in C++? When should I use one over the other?
Floating point precision in C++
I've heard that floating point numbers can sometimes be imprecise in C++. Can you explain why this happens and how to handle it?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant