C++ Logo

std-proposals

Advanced search

Re: [std-proposals] [DRAFT PAPER] std::variant with std::specify_base

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 14 Sep 2022 23:04:49 +0100
On Wed, Sep 14, 2022 at 8:27 PM Jason McKesson via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Wed, Sep 14, 2022 at 3:15 PM Lénárd Szolnoki via Std-Proposals
> <std-proposals_at_[hidden]> wrote:

> Well, there is a reason for this: it allows overloading `operator->`
> to access it. By explicitly specifying a type, you can create a single
> overload for that operator which returns a pointer to that specific
> type. If the type has to be computed, that would cause a problem if
> the `Ts` have multiple common bases.


Exactly.


> That having been said, I agree that a facade would be preferable, some
> type that references the variant and applies a specific base class
> test to them.


Do you mean a class that inherits from std::variant, or do you mean a
class that contains a member object which is an std::variant?


> I agree that the motivation section needs to be beefed up with more
> than what amounts to a declaration that people want to avoid dynamic
> allocations. I mean sure, they want to do that, but why are they using
> base class polymorphism? As you point out, having both a vtable
> pointer and a type discriminator in the object's storage isn't helping
> in RAM-limited systems.


If both "JPEG_File" and "Bitmap_File" have 128 bytes worth of char
array buffers inside them, then that adds up to 256 bytes for the two
objects. It's better to have 128 bytes + 1 byte (index) + 4 bytes
(vtable pointer) = 133 bytes (plus a couple of bytes more for the
Vtables and possibly also 'typeinfo').

If the base class were something like "Random_Number_Generator" and
one of the derived classes were "Mersenne_Twister", then we're up into
the kilobytes. I program a 32-Bit Arduino microcontroller with 92 kB
of SRAM in my day job . . . . and while I don't count dozens of bytes
here and there, I do count kilobytes here and there. They say "If you
take care of the pennies then the pounds will take care of themselves"
, but in the real world you'll be doing well just to focus on the
pounds and let the pennies slide. I only resort to penny-pinching when
I realise I've totally maxed out the RAM.

Received on 2022-09-14 22:05:01