I commonly use std::initializer_list<std::string>
, say {"foo"s, "bar"s}
, to fill a container.
Obviously, std::initializer_list<std::string>
contains temporary strings, and I can move them into a container:
#include <string>
#include <list>
using namespace std::literals;
void f(std::initializer_list<std::string> il)
{
std::list<std::string> coll;
for (auto&& tmp_str : il)
{
coll.emplace_back(std::move(tmp_str));
}
}
int main()
{
f({"foo"s, "bar"s});
}
However, according to cppref:
An object of type std::initializer_list is a lightweight proxy object that provides access to an array of objects of type const T.
Why does C++ make the element type of std::initializer_list const?
Aucun commentaire:
Enregistrer un commentaire