Boost logo

Boost :

Subject: Re: [boost] [variant2] Need rationale for never-empty guarantee
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2019-03-02 22:06:10


On Sat, Mar 2, 2019 at 3:49 PM Steven Watanabe via Boost
<boost_at_[hidden]> wrote:
>
> AMDG
>
> On 3/2/19 12:47 PM, Mathias Gaunard via Boost wrote:
> > On Fri, 1 Mar 2019 at 09:59, Peter Dimov via Boost <boost_at_[hidden]>
> > wrote:
> >
> >>
> >> Kind of, but as written this implies that std::variant has no costs, which
> >> is not true. The checks for valueless do carry a cost. Each visit(), for
> >> example, starts with `if(valueless) throw`, which is not necessary in
> >> variant2.
> >>
> >
> > That is not true.
> > A typical implementation would just add an extra value to the switch, there
> > is no extra branch.
> >
>
> That may be true in theory, but both libc++ and
> libstdc++ have if(__v.valueless_by_exception())
> scattered everywhere. Not to mention that neither
> uses switch.
>
> In Christ,
> Steven Watanabe
>

The code might say that, but the compiler probably still turns it into a switch.
The variant only has one internal variable, such as "_which". It
holds both the "is it A or B or C" and "or is it valueless".
So the resulting code should only need to look at it once (ie
something switch-like).

If compilers and std::variant haven't yet eliminated the cost of
valueless_by_exception on visitation, I think it is only a matter of
time before they do.

Tony


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