3

I wish to have a type that acts similarily to boost::any but with a more limited type set. Something like this:

limited_any<int,long,string> x; // x is like boost::any but is guaranteed to contain only an int, a long, or a string

How would you recommend implementing this? (Either on my own or using existing solutions)

tohava
  • 5,344
  • 1
  • 25
  • 47
  • 4
    You could look into how `boost::variant` does it. While `boost::any` is a typesafe equivalent to C's `void*`, `boost::variant` more resembles C's `union`. – Drew Dormann Feb 09 '15 at 15:30

1 Answers1

10

You are looking for boost::variant.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • 1
    @tohava Note that there are a few differences. The biggest implementation difference is that `any` is free-store based, while `variant` is automatic storage based. (Plus, `variant` has a richer type-safe interface than `any`) – Yakk - Adam Nevraumont Feb 09 '15 at 15:52