Boost logo

Boost :

Subject: Re: [boost] [variant] Opinion on boost::safe_get<> and default boost::get<> behavior
From: Olaf van der Spek (ml_at_[hidden])
Date: 2014-12-10 18:04:23


On Wed, Dec 10, 2014 at 3:57 PM, David Stone <david_at_[hidden]> wrote:
> I see no reason to allow code that is always a bug.
>
> There are two possibilities here:
>
> 1) The user is expecting the object of that type to be there, but used the
> wrong type. This is the example Matt Calabrese gave: the variant contains
> long but I accidentally used int. This is a bug.
>
> 2) The user is wondering whether the type is there and is trying to get it
> if it is. Perhaps they are checking multiple types in sequence. However,
> this is a performance bug if the compiler cannot remove the "never going to
> work" code. Even if the compiler can remove the code, people reading it
> might not be able to do so mentally. Now you have code that will never
> execute, but all readers of your code assume it will (or why would you have
> written it)? I have worked on code bases with lots of code like this, and I
> have never once appreciated the misleading rabbit holes this sends me down
> during debugging.

Compilers don't disallow if (0) and I think this construct could occur
in (generic) code. Could the same be true for the variant case?
I.e., is it always a bug?

-- 
Olaf

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk