C++ Logo


Advanced search

Re: [std-proposals] New method 'common_base' for 'std::variant'

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 7 Sep 2022 11:29:58 -0400
On Tue, Sep 6, 2022 at 1:34 PM Zhihao Yuan <zy_at_[hidden]> wrote:

> On Tuesday, September 6th, 2022 at 7:25 AM, Arthur O'Dwyer via
> Std-Proposals <std-proposals_at_[hidden]> wrote:
> This is just std::visit. Here's the "very simple" version using the Null
> Object pattern (i.e., creating a new dummy subclass of IRS232 to represent
> "no connection"):
> https://godbolt.org/z/rEW6aq744
> And here's the "less simple" version:
> https://godbolt.org/z/h1rjzxGKG
> There are other ways to do it too, but std::visit seems like the simplest
> — and it's already present in C++17.
> The logic is true, while the point of
> this proposal was probably to allow
> variant to return a pointer to its
> own buffer without breaking the
> type system.

My suggested use of `std::visit` doesn't "break the type system" as far as
I can tell.

*This specific use-case* can also be solved by using the return value of

if (chunked) {
g_pcoms = &g_coms.emplace<1>();
} else {
g_pcoms = &g_coms.emplace<2>();

So my conjecture that "std::visit seems like the simplest" was in fact
incorrect. But I'll re-conjecture that *this* version is about as simple as
it can get. ;)



Received on 2022-09-07 15:30:11