I would like to construct a unique_ptr
holding a vector<string>
, which contains single element only. Is it possible to do this in single line of code? I try this approach:
auto values = make_unique<vector<string>>({value});
But compiler (clang++) complains:
No matching function for call to 'make_unique' candidate template ignored:
substitution failure [with _Tp = std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >]: no type named '__unique_array_unknown_bound' in 'std::__1::__unique_if<st... candidate template ignored: substitution failure [with _Tp = std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >]: no type named '__unique_array_known_bound' in 'std::__1::__unique_if<std:... candidate function [with _Tp = std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >, _Args = <>] not viable: requires 0 arguments, but 1 was provided
I don't understand why it doesn't work since vector
apparently accepts using initializer_list
for construction.
string value = ...;
auto values = vector<string>({value}); // this works
Is creating a unique_ptr<vector<string>>
and calling its push_back
the only viable approach for constructing such a unique_ptr
?
auto values = make_unique<vector<string>>();
values->push_back(value);
More broadly, if I have a fixed number of elements (for example, 3 elements), and would like to build a unique_ptr
holding a vector with these elements, what is the idiomatic way to do it?
Aucun commentaire:
Enregistrer un commentaire