Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:22:58+00:00 2026-05-31T00:22:58+00:00

I am trying to initialise an std::vector<std::unique_ptr<std::string>> in a way that is equivalent to

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:22:59+00:00Added an answer on May 31, 2026 at 12:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get CMtoaPlugin::listArnoldNodes() to return an array of strings std::vector<std::string> ArnoldNodes =
trying to initialize a string from a vector. I am supposed to get hey
I am trying to assign data from a struct to a std::vector Here is
I'm trying to initialize a std::vector from a std::list efficiently, but I'm not having
I'm trying to design a class which has two vectors of large sequences. std::vector<double>
Gotw 80 includes the following example: // Example 1 // #include <string> using namespace
Am trying to inherit a class from a C++ vector and initialize it at
I am trying to create a struct that has multiple string arrays inside of
EDIT: Changed example below to one that actually demonstrates the SIOF. I am trying
I've been trying to initialize a map of <ints, vector<ints> > using the new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.