What does uniform / brace initialization change about narrowing conversions?
C++Level: IntermediateModern C++ Features
What does uniform / brace initialization change about narrowing conversions?
Hints
- Identify types, object lifetimes and compile-time decisions.
- Separate language guarantees from a particular compiler's behavior.
Answer
Uniform Initialization (Brace Initialization)
:Source: src/basic/uniform-initialization
Introduced in C++11, uniform initialization (also known as brace initialization)
provides a consistent syntax for initializing objects of any type. This syntax
works for primitives, aggregates, containers, and user-defined types. However,
the compiler prioritizes std::initializer_list constructors when applicable,
which can lead to surprising behavior if not understood properly.
Initializer list takes precedence:
When both a direct constructor and an std::initializer_list constructor
are viable, the compiler strongly prefers the initializer list version. In this
example, Widget{10, 5.0} invokes the initializer list constructor even though
Widget(int, double) appears to be a better match. The values 10 and 5.0
are implicitly converted to long double and passed as a two-element initializer
list. This behavior is mandated by the C++ standard to ensure consistent semantics
for brace initialization:
#include <iostream>
#include <initializer_list>
class Widget {
public:
Widget(int a, double b) { std::cout << "Direct constructor\n"; }
Widget(std::initializer_list<long double> il) { std::cout << "Initializer list constructor\n"; }
};
int main(int argc, char *argv[]) {
Widget w{10, 5.0}; // Output: Initializer list constructor
}
Narrowing conversions are prohibited:
Brace initialization prevents implicit narrowing conversions, providing an
additional layer of type safety compared to parentheses initialization. The
following code produces a compilation error because int and double
cannot implicitly narrow to bool without potential data loss. This
protection helps catch bugs at compile time that might otherwise cause
subtle runtime errors:
#include <initializer_list>
class Widget {
public:
Widget(int a, double b) {}
Widget(std::initializer_list<bool> il) {}
};
int main(int argc, char *argv[]) {
Widget w{10, 5.0}; // Compilation error: narrowing conversion
}
Fallback to direct constructor:
When no valid conversion exists for the initializer list element type,
the compiler falls back to the matching direct constructor. Here, int
and double cannot convert to std::string (there is no implicit
conversion path), so the compiler bypasses the initializer list constructor
entirely and selects the direct constructor instead:
#include <iostream>
#include <initializer_list>
#include <string>
class Widget {
public:
Widget(int a, double b) { std::cout << "Direct constructor\n"; }
Widget(std::initializer_list<std::string> il) { std::cout << "Initializer list constructor\n"; }
};
int main(int argc, char *argv[]) {
Widget w{10, 5.0}; // Output: Direct constructor
}
Source and licence: C/C++ Interview Cheatsheet — Chang Ning Tsai · MIT