6

How can I store in a std::vector multiple shared_ptr each one with a pointer to a different type?

std::vector < ? > vec;
vec.push_back( make_shared<int>(3));
vec.push_back( make_shared<float>(3.14f));

Is there a base polymorphic class that can I use for that task without having to use compiler-specific stuff?

7
  • How do you intend to get the type back of the stored class this way? Without a shared base class (the way I am used to seeing this done), you are not going to be able to tell what is stored if you mix floats and ints together. Some more background into what you are trying to accomplish would help as seeing these as shared pointers also has me scratching my head a bit. Jun 24, 2013 at 15:23
  • Can you explain why you need this?
    – dchhetri
    Jun 24, 2013 at 15:27
  • the vector does not need to be aware of the types, it is just a a place where to put "resources". I need at least 1 shared_ptr to keep resources alive, and the vector is here for that reason. Since there are several type of resources, I have to use several different shared_ptr... by the way I just solved the problem with a workaround. Jun 24, 2013 at 15:31
  • Just for a bit of variety, a non-boost solutions would be to use Variadic Templates, en.wikipedia.org/wiki/Variadic_template, which were a feature of C++11
    – Muckle_ewe
    Jun 24, 2013 at 15:33
  • 1
    @DarioOO: That's not what std::vector in itself is designed for. It is designed to be a container of T. Therefore, you either need to look for alternatives for std::vector, or reduce your problem to T entirely. Jun 24, 2013 at 15:39

6 Answers 6

7

There are a few ways you can do this. I assume you want to store various native types, as you're using int and float.

  1. If your list of types is finite, use boost::variant. e.g.

    std::vector<std::shared_ptr<boost::variant<int, float>>>;
    
  2. If you want to store anything, use boost::any. e.g.

    std::vector<std::shared_ptr<boost::any>>;
    
3
  • Is there a particular reason why you’re leaving a space at the beginning of the template argument list but not at the end? Jun 24, 2013 at 15:32
  • Because I'm a newb at formatting my posts. :-p They weren't showing up as code blocks initially, and the only way to get the editor to not gobble up my inner types was to leave a space.
    – bstamour
    Jun 24, 2013 at 15:34
  • 1
    Ah. :) Makes sense. I corrected the code blocks but I didn’t want to mess with your idiomatic code style, even though I found it atrocious. ;-) Glad to hear that we agree on that. Jun 24, 2013 at 15:37
0

I you really need to do exactly this, use boost::any.

And use std::vector <std::shared_ptr<boost::any> > vec;

0

You can't mix types like that. You may want to have a look at boost::any

0

No, C++ does not have a single type from which everything is derived like many other languages do. int and float are native types, meaning they are fundamental to the language and not derived from anything else.

If you have the benefit of Boost, you can store any or variant types in your vector instead.

0

I need at least 1 shared_ptr to keep resources alive

For this to work, all the types you store must inherit from the same base class, which is the same type as the type contained in the shared pointer. Additionally, the base class must declare a virtual destructor (see this question).

Using boost::any will mean that every other shared_ptr to the objects also has to be a shared_ptr<bsst::any>, which is likely not what you want.

0

A possible way I figured to solve that is by doing the following

class ISomething{
    bool isAlive;
public:
    virtual bool alive(){ return isAlive;}
    virtual ~ISomething() {}
};

template <typename T>
class Something: public ISomething{
    std::shared_ptr<T> myRes;
public:    
};

std::vector<ISomething*> myVec; //push back a new dynamic allocation for each "Something"

That's basically type erasure, the same concept behind "Boost::Any" "Poco::Any" "Ogre::Any"

2
  • 1
    This won't work due to a concept called object slicing.The vector ONLY stores the ISomething part of the objects. Trying to store a Something will result in only the ISomething part of the Something being copied into the vector. When the source Something goes out of scope it will be deleted, releasing the reference count on the shared_ptr<>. If you instead heap allocated the Something and stored std::vector<ISomething*> it would work, but thats probably not what you want.
    – Mark
    Dec 2, 2014 at 15:53
  • Thanks for diggin that, yes, basically I needed "type erasure" but was doing that in a incorrect way. Forgot about this answer, I'll correct now Dec 2, 2014 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.