C++ Logo


Advanced search

Re: std::variant - going from Alternative& to the enclosing variant&

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Fri, 11 Sep 2020 14:05:12 +0200
pt., 11 wrz 2020 o 02:03 Ville Voutilainen
<ville.voutilainen_at_[hidden]> napisaƂ(a):
> On Fri, 11 Sep 2020 at 02:49, Marcin Jaczewski via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> > One thing, why do you not implement `variant_from_alternative`
> > yourself? If standard can do it, then you can do it too.
> Incorrect. The layout of variant's data is not accessible that way to
> client code.

Ok, I did not pay enough attention to what this function do and
requires, and if we need access to internal state then only std lib
can fix it.

Btw this means this function is cast? And if I use the wrong variant
type then we will have UB?

> > If you can't do it then is it possible for a standard library to do it
> > correctly?
> Yes. There's nothing to it, the stdlib vendors know their variant's
> internals, and can convert
> a reference to a variant member to a variant without any trouble.

Received on 2020-09-11 07:08:53