Using push_back/ emplace_back(rare push_front/ emplace_frontor even push_after/ emplace_after), I can fill almost any container from STL. Even a container of custom items. Reducing the size only requires the presence of element destructors (moreover, containers necessarily require that the elements be Destructible ). But I canβt just use resize(if present) to reduce the size of the containers of non-standard default elements. Instead, I have to use a workaround, even for the least demanding (to their special features) std::list.
#include <list>
#include <cstdlib>
int
main()
{
std::list< std::reference_wrapper< int > > l;
int i{};
l.emplace_back(i);
int j{};
l.emplace_back(j);
std::size_t const size = l.size();
int k{};
l.emplace_back(k);
for (std::size_t s = l.size(); size < s; --s) {
l.pop_back();
}
return EXIT_SUCCESS;
}
std::list::resize std::list::value_type DefaultInsertable.
, resize(std::size_t) " " . resize(std::size_t, value_type = value_type()) (, ) resize(std::size_t)/resize(std::size_t, value_type), value_type DefaultConstructible. , , resize(std::size_t) , , std::bad_alloc. ?