C++ Logo


Advanced search

Re: [std-proposals] Chimeric Pointer

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 28 Nov 2022 13:38:44 -0800
On Monday, 28 November 2022 12:34:55 PST Marcin Jaczewski via Std-Proposals
> > protected:
> > std::tuple<Bases*...> pointers;
> If we need 10 bases this class could be bigger than the original object.
> Probably a better solution would be to store an offset in some static
> memory and add this offset to the `void*` pointer.

Such a concept already exists: it's called "base class". If you store a common
ancestor to all the bases, then you can dynamic_cast from it to the requested
base and the operator will do the necessary verifications and pointer
adjustments for you.

Of course, this requires that there is a common ancestor (that is, a diamond
inheritance pattern). If instead the classes are in a multi-root hierarchy, it
won't work. Your idea of generating a static function would:

> chimeric_ptr(T* ptr) : offset{ [p = ptr](int i) { switch(i) {
> case 0: return (void*)static_cast<BaseA*>(p); case 1: return
> (void*)static_cast<BaseB*>(p); } } }

I'd just make it stateless (don't capture ptr).

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2022-11-28 21:38:47