You’re observing a subtle but important aspect of C++'s initialization rules, particularly how braced-init-lists ({}) are handled in different contexts. Let's break down what's happening in each scenario you described.
1. Returning {1, 2} in a Function
std::vector<int> foo() {
return {1, 2};
}
What Happens Here:
- Contextual Type Information: The
return statement knows that the function's return type is std::vector<int>.
- List Initialization: The
{1, 2} is interpreted as an std::initializer_list<int>, which std::vector<int> can be constructed from.
- Implicit Conversion: The compiler uses the appropriate constructor of
std::vector<int> that takes an std::initializer_list<int> to create and return the vector.
Key Point: In this context, the braced-init-list is contextually converted to the function’s return type (std::vector<int>) using the appropriate constructor.
2. Using auto with a Braced-Init-List
auto bar = {1, 2};
std::vector<int> bax = bar; // Error
std::vector<int> qux(bar); // Error
What Happens Here:
- Type Deduction with
auto: When you write auto bar = {1, 2};, the compiler deduces bar to be of type std::initializer_list<int>, not std::vector<int>.
- No Implicit Conversion from
std::initializer_list<int> to std::vector<int> in Assignments:
- Assignment (
bax = bar): std::vector<int> does not have an assignment operator that takes an std::initializer_list<int> implicitly from another std::initializer_list<int>.
- Constructor (
qux(bar)): Similarly, std::vector<int> does not have a constructor that takes an std::initializer_list<int> from another std::initializer_list<int> implicitly.
Key Point: When using auto with a braced-init-list, the type is deduced as std::initializer_list<int>, and there is no implicit conversion to std::vector<int> in this context.
Why the Difference?
How to Achieve the Desired Behavior Without Errors
If you want to initialize a std::vector<int> directly, you can do so without using auto:
std::vector<int> bax = {1, 2}; // Direct list initialization
std::vector<int> qux({1, 2}); // Using constructor syntax
std::vector<int> quux = std::vector<int>{1, 2}; // Explicit type
Alternatively, if you want to use auto and still have a std::vector<int>, you can explicitly specify the type:
auto bax = std::vector<int>{1, 2};
Summary
return {1, 2}; works because the compiler uses the function’s return type to interpret the braced-init-list appropriately.
auto bar = {1, 2}; deduces bar as std::initializer_list<int>, which cannot be implicitly converted to std::vector<int> in assignments or constructors without explicit casting or constructors.
Understanding these nuances of C++ initialization and type deduction helps in writing more predictable and error-free code.
References:
When you return a braced-init-list from a function, C++ uses the function’s return type to interpret the list and construct the appropriate object (like std::vector) using list-initialization. This isn’t a cast but contextual initialization based on the target type. In contrast, when you use auto with {1, 2}, it deduces an initializer_list, which can’t implicitly convert to std::vector. So, the return statement leverages the expected return type to initialize the object from the initializer list.