Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing container of unique_ptrs from initializer list fails with GCC 4.7

I am trying to initialise an std::vector<std::unique_ptr<std::string>> in a way that is equivalent to an example from Bjarne Stroustrup's C++11 FAQ:

using namespace std; vector<unique_ptr<string>> vs { new string{"Doug"}, new string{"Adams"} }; // fails unique_ptr<string> ps { new string{"42"} }; // OK 

I can see no reason why this syntax should fail. Is there something wrong with this way of initializing the container?
The compiler error message is huge; the relevant segment I find is below:

/usr/lib/gcc-snapshot/lib/gcc/i686-linux-gnu/4.7.0/../../../../include/c++/4.7.0 /bits/stl_construct.h:77:7: error: no matching function for call to 'std::unique_ptr<std::basic_string<char> >::unique_ptr(std::basic_string<char>&)'

What is the way to fix this error ?

like image 308
juanchopanza Avatar asked Mar 08 '12 13:03

juanchopanza


1 Answers

unique_ptr's constructor is explicit. So you can't create one implicitly with from new string{"foo"}. It needs to be something like unique_ptr<string>{ new string{"foo"} }.

Which leads us to this

// not good vector<unique_ptr<string>> vs {     unique_ptr<string>{ new string{"Doug"} },     unique_ptr<string>{ new string{"Adams"} } }; 

However it may leak if one of the constructors fails. It's safer to use make_unique:

// does not work vector<unique_ptr<string>> vs {      make_unique<string>("Doug"),      make_unique<string>("Adams") }; 

But... initializer_lists always perform copies, and unique_ptrs are not copyable. This is something really annoying about initializer lists. You can hack around it, or fallback to initialization with calls to emplace_back.

If you're actually managing strings with smart pointers and it's not just for the example, then you can do even better: just make a vector<string>. The std::string already handles the resources it uses.

like image 148
R. Martinho Fernandes Avatar answered Sep 17 '22 17:09

R. Martinho Fernandes