C++ Quiz28 of 173
Difficulty:
⬅️ Prev: 33Quiz ID: 35Next: 37 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>
#include <vector>

int main() {
  std::vector<int> v1(1, 2);
  std::vector<int> v2{ 1, 2 };
  std::cout << v1.size() << v2.size();
}
Hint

Which constructors are invoked for v1 and v2? How is a vector constructed from an initializer list?

Reveal answer

12

Explanation

To answer this we need to look at overload resolution of vector's constructors:

§[vector.cons]¶7 says:

constexpr vector(size_type n, const T& value,
                 const Allocator& = Allocator());

Effects: Constructs a vector with n copies of value, using the specified allocator.

So v1 contains one "2".

§[over.match.list] says (in summary) that when non-aggregate classes (such as vector) are list-initialized† and have an initializer list constructor (again, like vector), that constructor is chosen, and the argument list consists of the initializer list as a single argument. (†: §[dcl.init.list]¶1: "List-initialization is initialization of an object or reference from a braced-init-list".)

So v2 is initialized from the elements (aka initializer-clauses) in the braced-init-list, and contains the elements "1" and "2".

⬅️ Prev: 33Quiz ID: 35Next: 37 ➡️