In this post, we continue discovering the changes introduced by C++23. We are going to look into three (and a half) small changes, each affecting constructors of some standard library types. We’re going to see how new constructors for container types, a new range constructor for string_view
and some default template arguments for pair
.
C++23: More Small Changes
by Sandor Dargo
From the article:
As of C++20, almost all container-like objects (containers and container-adaptors) can be initialized with a pair of iterators.
std::vector<int> v{42, 51, 66}; std::list<int> l(v.begin(), v.end());All of them, except forstd::stack
andstd::queue
. They don’t provide such overloads. If you want to initialize them with a pair of iterators, you need an intermediarystd::initiailizer_list
.
std::vector<int> v{42, 51, 66}; // std::queue<int> q1(v.begin(), v.end()); // DOESN'T COMPILE! std::queue<int> q2({v.begin(), v.end()});This inconsistency, at first, looks like a small inconvenience. But its effects are much deeper. While using the
stack
orqueue
on its own is not a big burden, if you want to offer functionality that works with all container-like objects, you have a higher price to pay.Due to the lack of an iterator-pair-based constructor, you either have to ...
Add a Comment
Comments are closed.