This question is related to decltype and multiple inheritance.
Suppose I have the following:
- abstract class A with several virtual methods,
- several derived classes that implement methods using the previous virtual ones (each of these classes is a precedent)
- a final concrete class that inherits from a subset of previous use cases and implements pure virtual methods.
For instance:
#include <iostream> /** * "Iterable container" */ template <class T> struct A { virtual T* data() =0; virtual const T* data() const =0; virtual unsigned size() const =0; T* begin() { return data(); } T* end() { return data()+size(); } const T* begin() const { return data(); } const T* end() const { return data()+size(); } }; // ------------------------------------------------------------------------ /** * Iterative assignment */ template <class T> struct B: public A<T> { auto operator =( const T& val ) -> decltype(*this) { for ( auto& v: *this ) v = val; return *this; } }; /** * Iterative display */ template <class T> struct C: public A<T> { void show() const { for ( auto& v: *this ) std::cout<< v << " "; std::cout<< std::endl; } }; // ------------------------------------------------------------------------ /** * Concrete implementation */ template <class T, unsigned N> struct D: public B<T>, public C<T> { using B<T>::operator=; T dat[N]; T* data() { return dat; } const T* data() const { return dat; } unsigned size() const { return N; } }; // ------------------------------------------------------------------------ int main() { D<double,5> d; (d = 42).show(); // compile-time error, "no member named 'show' in 'B<double>'" }
The problem is that this is not a pun; if one of the use-case methods should return a reference to *this , I would like this be a reference to the final concrete class, so that I could link the call to other methods from other use cases.
However, with the previous implementation, I get a compile-time error. Is there any other way to achieve what I explained?
source share