I have defined a boost::variant var like this:
boost::variant<boost::blank, bool, int> foo;
This variable, when instantiated but not initialized, has a value of type boost::blank
, because boost::blank
is the first type passed to the templated boost::variant.
At some point, I want to know if foo
has been initialized. I've tried this, but with no good results:
if (foo) //doesn't compile
if (foo != boost::blank()) //doesn't compile
if (!(foo == boost::blank())) //doesn't compile
I think it's worth noticing that, when foo
has been initialized (eg., foo = true
), it can be "reset" by doing foo = boost::blank();
.
How can I check if foo
has been initialized, ie, it has a different type than boost::blank
?
bool const is_blank = boost::get<boost::blank>(&foo)
– Combativeboost::variant<comment, answer> foo(getWhatThatShouldHaveBeen()); assert(foo.which() == 1);
– Nostologyboost::get
performs astatic_cast
and applies a visitor if the cast is successful. Would a visitor of typeboost::blank
be so trivial that I could neglect the overhead? – Shabuothwhich
worked differently. There really is no need to be pushy. Your answer is cheap and valid, but could eventually cause problems. I'll add a comment to your answer to discuss that. – Shabuothwhich
for visit functions. I don't think there would be significant performance difference to check againstwhich()
. – Combative