On Tue, Sep 6, 2022 at 1:34 PM Zhihao Yuan <zy@miator.net> wrote:
On Tuesday, September 6th, 2022 at 7:25 AM, Arthur O'Dwyer via Std-Proposals <std-proposals@lists.isocpp.org> 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"):
And here's the "less simple" version:

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 `emplace`:

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. ;)