I'm just wondering how to implement boost::variant .
Does it work like this?
Two members:
- A number representing the current saved type (i.e. 0 for the first template parameter, 1 for the second template parameter, etc.)
- The union of all possible types (which, of course, is the size of the largest).
apply_visitor() :
It has a switch for a number representing the current stored type to invoke the correct overload (this will be compiled as a jump table in the worst case, so constant time will be required).
I also understand that there are a number of optimizations that can be sure that boost::variant does not need to dynamically allocate memory as detailed here , but I think I get them.
c ++ boost boost-variant
Clinton
source share