On 4/13/19 2:34 PM, Alf P. Steinbach wrote:
> On 13.04.2019 18:42, bitrex wrote:
>> So there's static polymorphism using e.g the CRTP that allows
>> logic customization of derived types of an interface at compile time,
>> but does not allow for dynamic dispatch. This has low (in theory zero)
>> overhead as compared to runtime polymorphism.
>
> CRTP can give code duplication overhead.
>
> In the early days that was a real issue, and one wrote template classes
> with strict client side type checking as wrappers around a single
> `void*` state based implementation. Whose code was not duplicated.
>
> As I recall it was Andrei Alexandrescu who first noted, somewhere in the
> late 200x?, that compilers had progressed to a point where code
> duplication, template code bloat, was no longer a serious issue.
The CRTP I like a lot for resource-constrained embedded processors. On
modern compilers it really does seem to allow compile-time interface ->
implementation specialization with no overhead. Tools like Compiler
Explorer show me what code actually generates some asm and what doesn't.
what generally happens in most of my use cases is the appropriate calls
are all figured out and inlined where they need to go, none of the
template-tized interface "stuff" generates anything.
>> And there's virtual methods which allow for dynamic dispatch of
>> overloaded derived class methods through a base class pointer, in
>> theory to an "infinite" number of overloads in various derived
>> classes. And has the usual vtable + dynamic dispatch overhead.
>>
>> What I'm wondering is if there's some known hybrid approach that
>> allows dynamic dispatch-type behavior from a base type pointer, with
>> the lower overhead of static polymorphism, given the constraint (has
>> to be a constraint somewhere I guess or you're just talking about
>> regular virtual based runtime polymorphism, again) that it works only
>> with a limited subset of potential subclasses designed expressly for
>> that purpose.
>>
>> Or some other fashion of constraint.
>
> One can always dispatch on a class id.
>
> But that's an application level indirection, to avoid a core language
> level indirection, so not likely to be faster.
The use case for me is essentially not so much speed (the execution
cycle overhead of a vtable lookup and indirection on a modern RISC
processor is pretty darn low) but, in relatively resource constrained
embedded processors where, because reasons, vtables are copied to SRAM
when they don't strictly need to be. And if I have a lot of small class
instances the extra 4 or 8 bytes for a hidden pointer can become a
significant part of the total object size.
But sometimes I do just need to have base pointers stored in a list
where at runtime I don't know what type of derived class the pointer is
pointing to and being able to do a virtual call just makes life so much
easier.
>> Or would any solution of that type simply also be user-implemented
>> virtualization "in disguise." I have read a bit about the "Visitor"
>> pattern which seems relevant to my question but don't know enough to
>> know for sure. Thanks
>
> The visitor pattern just avoids or (depending on the case) centralizes
> downcasting.
>
> Again there's Andrei: he wrote at length about visitor pattern variants
> in his now classic Modern C++ Design book.
>
I have a copy somewhere around here, it's a pretty dense book so I've
been working my way thru it. Haven't gotten there yet I guess...
>
> Cheers!,
>
> - Alf
>
>