mardi 28 janvier 2020

Initializing std::vector with curly braces uses initializer list

When I replace inner parens with curly braces I get a different result. Why?

Aren't they equivalent after C++11? (apart from preventing integer demotion) Why does it change construction:

from std::vector(size_type count, const T& value = T(), const Allocator& alloc = Allocator());

to std::vector(std::initializer_list<T> init, const Allocator& alloc = Allocator());

EXAMPLE

auto th_buckets = std::vector<std::vector<int>>{2, std::vector<int>(5, 0)};

0 0 0 0 0

0 0 0 0 0

auto th_buckets = std::vector<std::vector<int>>{2, std::vector<int>{5, 0}};

5 0

5 0

Aucun commentaire:

Enregistrer un commentaire