Leroy
Leroy

Reputation: 544

initialize struct with shared_ptr<void>

I keep running into an error that no matching constructor for initialization of 'std::shared_ptr<void>' which makes sense, but I don't know where to begin.

Here is what I'm working with.

#include <memory>                                                                                                                                                             

struct Container {                                                             
  int type;                                                                    
  std::shared_ptr<void> payload;                                               

  Container(int t, const void *p) : type(t), payload(p) { }                    
};                                                                             

int main() {                                                                   
  return 0;                                                                    
}

I'm trying to make a generic container using a shared_ptr with type of void. I was going to do a switch on the type and then just cast the payload into the appropriate type.

I figured I could just do something like Container ctn(1, new Data()); but I think I might have that wrong as well.

Thanks.

Upvotes: 4

Views: 1264

Answers (3)

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17424

Note up front: Take a look at Boost.Any, which probably does what you want.

That said, you can't just store a void pointer in a shared_ptr, because delete on a void pointer doesn't work. For that reason, shared_ptr overloads its constructor in order to create the right deleter for the actual type of the pointer. You could do the same using a template constructor:

struct Container {
    int type;
    std::shared_ptr<void> payload;
    template<typename T>
      Container(int t, T* p) : type(t), payload(p) { }
};

Now, have fun finding out why this is a recipe to shoot yourself in the foot.

Upvotes: 2

dynamic
dynamic

Reputation: 48131

You can use a template and write shared_ptr<T>

If you don't want to use a template then you should define an abstract class and all the items you will put in the container should be derived from that class (as a java interface)

But I strongly discourage you to use void*

Upvotes: 1

You're trying to initialise a pointer to void with a pointer to const void, which is indeed illegal. The "smartness" of the pointer does not matter, this would fail for ordinary pointers as well.

You either need to change the type of payload to std::shared_ptr<const void>, or (probably more what you actually want) change the type of p to void*.


Note, however, that while this will technically solve the immediate problem, it can introduce a more fundamental one. Under this design, payload will know nothing about the actual type to which it points, so it will not call a destructor of Data when the last reference goes out. You should probably rethink the entire design to use suitably typed pointers.

Upvotes: 2

Related Questions