How operator<< with boost::variant is implemented

Viewed 291

I understand that boost::variant is implemented something like so

template <typename... Vs>
struct variant {
    std::aligned_union<Vs...>::type buffer;
    ....
};

How can we make an operator<< for a struct like this that prints the casts the type stored in the buffer and passes that to operator<< for cout? For this we would need to know the type of the element stored in the buffer right? Is there a way to know this?

Also I am looking for an explanation of such an implementation, if one exists. Not just that it exists and how I can use it.

1 Answers
Related