Boost logo

Boost :

Subject: Re: [boost] [variant] Opinion on boost::safe_get<> and default boost::get<> behavior
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2014-12-09 15:54:10

On Dec 9, 2014 10:33 AM, "Peter Dimov" <lists_at_[hidden]> wrote:
> That's not true. Both get variants can fail (at runtime) if the variant
doesn't contain the target, so your code that uses either MUST be prepared
to deal with this case. The difference is only that one of them allows you
to ask "does this variant<X,Y> contain a Z?" (answering "no"), while the
other does not.

Right, but again, this is analogous to something like static_cast allowing
you to cast between unrelated types and not telling you until runtime.
Perhaps I should have related it to dynamic_cast if that makes things more
clear. You need some functionality at run-time, of course, but I don't know
if you gain anything tangible by allowing the code to compile for unrelated
types for which the conversion is known at compile-time to never be
possible. It's hard to say without use-cases, though. I use variants a lot
and I don't see this as desirable, but I may have just not encountered a
use. If I have an integer long in a variant and I accidentally try to grab
an int, I'd certainly like to know at compile-time that the mistake was

Boost list run by bdawes at, gregod at, cpdaniel at, john at