Date: Sat, 11 Mar 2023 13:34:30 +0100
I noticed that C++23 added new overloaded constructors for the std::stack
and std::queue container adapters, that allow to construct the underlying
container with the contents of the range [first, last).
In cppreference, it is also shown how these overloaded constructors can be
used with std::initializer_list, and are provided the following examples:
( 1 )
const auto il = {2, 7, 1, 8, 2};
std::stack<int> c4 { il.begin(), il.end() };
const auto il = {2, 7, 1, 8, 2};
std::queue<int> c4 { il.begin(), il.end() };
However, C++23 also added other container adapters (std::flat_set,
std::flat_multiset, std::flat_map, std::flat_multimap), that provides the
following overloaded constructors:
/* flat_container */(initializer_list<key_type> il, const key_compare& comp
= key_compare())
: /* flat_container */ (il.begin(), il.end(), comp) { }
template<class Allocator>
/* flat_container */(initializer_list<key_type> il, const key_compare&
comp, const Allocator& a);
template<class Allocator>
/* flat_container */ (initializer_list<key_type> il, const Allocator& a);
I did not find a specific reason therefore the overloaded constructors were
introduced in these group of container adapters but not in the others.
My observations are the followings:
- the absence of overloaded constructors for std::initializer_list requires
using the hack ( 1 );
- passing directly a std::initializer_list would involve constructing
in-place the container type, if possible;
- since some container adapters also use overloaded constructors for
std::initializer_list, there is not reason not to introduce them into the
other container adapters.
and std::queue container adapters, that allow to construct the underlying
container with the contents of the range [first, last).
In cppreference, it is also shown how these overloaded constructors can be
used with std::initializer_list, and are provided the following examples:
( 1 )
const auto il = {2, 7, 1, 8, 2};
std::stack<int> c4 { il.begin(), il.end() };
const auto il = {2, 7, 1, 8, 2};
std::queue<int> c4 { il.begin(), il.end() };
However, C++23 also added other container adapters (std::flat_set,
std::flat_multiset, std::flat_map, std::flat_multimap), that provides the
following overloaded constructors:
/* flat_container */(initializer_list<key_type> il, const key_compare& comp
= key_compare())
: /* flat_container */ (il.begin(), il.end(), comp) { }
template<class Allocator>
/* flat_container */(initializer_list<key_type> il, const key_compare&
comp, const Allocator& a);
template<class Allocator>
/* flat_container */ (initializer_list<key_type> il, const Allocator& a);
I did not find a specific reason therefore the overloaded constructors were
introduced in these group of container adapters but not in the others.
My observations are the followings:
- the absence of overloaded constructors for std::initializer_list requires
using the hack ( 1 );
- passing directly a std::initializer_list would involve constructing
in-place the container type, if possible;
- since some container adapters also use overloaded constructors for
std::initializer_list, there is not reason not to introduce them into the
other container adapters.
Received on 2023-03-11 12:34:43