Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CRTP mix-ins

16 views
Skip to first unread message

bitrex

unread,
Sep 16, 2018, 3:31:47 PM9/16/18
to
I was reading this blog post about "mix-in" classes which combine
aspects of runtime polymorphism/dynamic dispatch, e.g. having a pure
virtual base class such that base pointers can hold pointer-to-derived
and call derived overloads, and static polymorphism where a particular
implementation's overloaded behavior is defined at compile time thru a
template "mix-in."

This lead me to thinking about how it could be combined with the CRTP in
some way. Specifically to perhaps address a problem I see with the
implementation shown there which is that the abstract methods defined in
class "Animal" in the example cannot easily return
implementation-defined custom types.

This is a sketch of an idea where the purpose is to have a CRTP
Interface you can plug an implementation into whereby if you use a
factory method to create an object by value you have a full set of
static polymorphism implementation methods available, if you use a
factory method to create a new pointer to base and put the
implementation there you have at least a subset of its CRTP-defined
implementation available to call via the -> operator on the base
pointer, and may return a user defined type of type T.

This is not working code, I messed around with variations on this for a
while yesterday and couldn't come up with anything that was very
satisfactory. Perhaps it isn't possible to do easily without a wrapper
container like std::function (not always available in the standard
library for some of the devices I use.)

Suggestions regarding other approaches to accomplish something in the
mix-in "spirit" I'd also be very glad to look at

class FooBase {
public:
virtual FooBase() = default;

template <typename T, ? ? ? ? > T do_a_foo() const { ? ? ? ? ? }

private:
virtual ????? something_that_helps_do_a_foo() const = 0;

}

template <typename T, template <typename Impl> class FooInterface {
public:
T do_a_foo() {
auto helper =
static_cast<Impl<T>*>(this)->something_that_helps_do_a_foo()();
return do_something(helper);
}
};

template <typename T>
class FooImplementation : public FooInterface<T, FooImplementation>,
public FooBase {
friend class FooInterface<T, FooImplementation>;

private:
???? something_that_helps_do_a_foo const override () { return ????? }
};

bitrex

unread,
Sep 16, 2018, 3:32:36 PM9/16/18
to
On 09/16/2018 03:31 PM, bitrex wrote:
> I was reading this blog post about

Apologies the post I'm referring to is here:

<https://michael-afanasiev.github.io/2016/08/03/Combining-Static-and-Dynamic-Polymorphism-with-C++-Template-Mixins.html>
0 new messages