One method for making sure that you have a well defined variant is to include a "NullType" in your variant list. While it may neccessitate writing more code in the "visitors" you will write to use it, they can throw exceptions to let operators know something is amiss. I'm generally against such runtime checks but sometimes, there really isn't any other way. Suffice to say that:
class NullType{};
Then add it as the very first argument to the variant list. As others have said and the boost documentation describes you'll never have a situation where a variant is empty. However, you can do a type check to ensure that you won't ever be able to compile with "NullType" if you don't overload functions or have a runtime exception thrown if you do have a "NullType".
Now your variant:
boost::variant<NullType, int, double, long double> number;
class DoSomething : boost:static_visitor<void>{
public:
void visit(const int& _item);
void visit(const double& _item);
void visit(const long double& _item);
void visit(const NullType& _uhOh);
};
boost::variant
seems not to get empty... – Summer