Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sighingnow/6f3a9be7d95a3ef330372e1a62d16a32 to your computer and use it in GitHub Desktop.
Save sighingnow/6f3a9be7d95a3ef330372e1a62d16a32 to your computer and use it in GitHub Desktop.
Fixes the incomplete type and ambiguous dependent name problem for CRTP: https://godbolt.org/z/3WGe1q5K5
template <typename U>
struct A_trait {
using T = int;
};
template <typename U, template <typename ...> class Trait = A_trait>
struct A {
using T = typename Trait<U>::T;
};
template <typename U>
struct B_trait {
using T = int;
};
template <typename U, template <typename ...> class Trait = B_trait>
struct B {
using T = typename Trait<U>::T;
};
template <typename U>
struct C_trait {
};
template <typename U, template <typename ...> class Trait = C_trait>
struct C {
using T = typename Trait<U>::T;
};
// using multiple inherits to implements multi-inherits without the ambiguous error
//
// asusme the ambiguous dependent types are actually the same type
template <typename U>
struct Trait0 {};
template <typename U>
struct Trait1: public Trait0<U>, public A_trait<U> {};
template <typename U>
struct Trait2: public Trait1<U>, public B_trait<U> {};
template <typename U>
struct Trait: public Trait2<U>, public C_trait<U> {};
// pass the final trait as the type parameter
struct K: public A<K, Trait>, public B<K, Trait>, public C<K, Trait> {
};
int main() {
K k{};
K::T x{};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment