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

  • SEARCH
  • Home
  • 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 7049479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:01:16+00:00 2026-05-28T03:01:16+00:00

A container of unique_ptr seems to make little sense: you cannot use it with

  • 0

A container of unique_ptr seems to make little sense: you cannot use it with initializer lists and I failed to iterate through the container (workarounds below). Am I misunderstanding something? Or when does it make sense to use unique_ptr and STL containers?

#include <memory>
#include <vector>

using namespace std;

struct Base { void go() { }  virtual ~Base() { } }; 
// virtual ~Base() = default; gives
// "declared virtual cannot be defaulted in the class body" why?

class Derived : public Base { };

int main() {

  //vector<unique_ptr<Base>> v1 = { new Derived, new Derived, new Derived };
  //vector<shared_ptr<Base>> v2 = { new Derived, new Derived, new Derived };
  vector<Base*> v3 = { new Derived, new Derived, new Derived };
  vector<shared_ptr<Base>> v4(v3.begin(), v3.end());
  vector<unique_ptr<Base>> v5(v3.begin(), v3.end());

  for (auto i : v5) { // works with v4
    i->go();
  }
  return 0;
}

The following questions helped me find these workarounds:

  • How to initialize a container of noncopyable with initializer list?

  • Can I list-initialize a vector of move-only type?

  • when I need containers of NoCopy types I usually use boost::ptr_vector or std::vector<shared_ptr>

  • 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-28T03:01:17+00:00Added an answer on May 28, 2026 at 3:01 am
    for (auto i : v5) {
      i->go();
    }
    

    Should be

    for (auto& i : v5) { // note 'auto&'
      i->go();
    }
    

    Else you’ll try to copy the current element.

    Also, you can’t use an initializer list like that, because the constructors of std::unique_ptr and std::shared_ptr are marked explicit. You need to do something like this:

    #include <iterator> // make_move_iterator, begin, end
    
    template<class T>
    std::unique_ptr<T> make_unique(){ // naive implementation
      return std::unique_ptr<T>(new T());
    }
    
    std::unique_ptr<Base> v1_init_arr[] = {
        make_unique<Derived>(), make_unique<Derived>(), make_unique<Derived>()
    };
    
    // these two are only for clarity
    auto first = std::make_move_iterator(std::begin(v1_init_arr));
    auto last = std::make_move_iterator(std::end(v1_init_arr));
    std::vector<std::unique_ptr<Base>> v1(first, last);
    
    std::vector<std::shared_ptr<Base>> v2 = {
        std::make_shared<Derived>(),
        std::make_shared<Derived>(),
        std::make_shared<Derived>()
    };
    

    And this is a Good Thing™, because otherwise you might leak memory (if one of the later constructors throws, the former ones aren’t yet bound to the smart pointers). The tip-toeing for the unique_ptr is necessary, because initializer lists copy their arguments, and since unique_ptrs aren’t copyable, you’d get a problem.


    That said, I use a std::map<std::string, std::unique_ptr<LoaderBase>> for a dictionary of loaders in one of my projects.

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

Sidebar

Related Questions

Suppose I have some data stored in a container of unique_ptr s: struct MyData
I'm trying to use Boost's multi-index container for fast look up, but I'm having
Problem I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T>
Creating an object and giving ownership to a container using a unique_ptr is no
I like to know why can not the container just use IP address of
How does one access unique_ptr elements of a container (via an iterator) without taking
I have a unique little problem. I have to show and hide a container,
I use Windsor container in purpose of SolrNet multicore access, but my Solr cores
I am unable to decide which STL container to use in the following case:
$(.container).hover( function(){ $(.child-1).hide(0); $(.child-2).show(0); },function(){ $(.child-1).show(0); $(.child-2).hide(0); }); A project I have requires that

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.