2

I'd like to use boost::any as a universal type to store in a container and to pass to different functions. But inside these functions I always know the real type, so in runtime I don't need any type safety checks and the imposed overhead, static_cast is enough.

The ideal solution would be to use something like boost::polymorphic_downcast, but as I can see it can't be applied in this case.

Should I just write my own wrapper for void* or is there another option?

lizarisk
  • 7,562
  • 10
  • 46
  • 70

1 Answers1

1

You may use shared_ptr<void> or unique_ptr with static_cast to replace boost::any, shared_ptr<void> has several advantage over raw void* pointer like:

  • automatically delete its storage
  • can referenced by multiple object in your code(may be you don't need this)

But of course in this case you need more memory for every pointer(for reference counting and deleter).

if sharing is not good for you and your object will only owned in container you may also use unique_ptr<void>.

BigBoss
  • 6,904
  • 2
  • 23
  • 38