0

I have a code snipped including a variadic mixin crtp of some sorts and a few related questions. Do I understand correctly that in the following code, the second constructor is merely passing copies of instances of those classes which were used to instantiate X to the constructors of those very same classes?

template<class DerivedT>
struct CuriousBase{};

template<template<typename> typename... Features>
struct X : Features<X<Features...>> ... {
  X() = default;
  X(Features<X<Features...>> ...f) : Features<X<Features...>>(f)... {}
};

int main(){
  auto x = X<CuriousBase>{};
}

what are use cases for such a behaviour and what is the difference between this sample and the following snippet?

template<class DerivedT>
struct CuriousBase{};

template<template<typename> typename... Features>
struct X : Features<X<Features...>> ... {
  X() = default;
  X(Features<X> ...f) : Features<X>(f)... {}
};

int main(){
  auto x = X<CuriousBase>{};
}

Both do compile but I thought, that one has to specify explicitly that X is itself a variadic class template but that does not seem to be necessary...

help is greatly appreciated

Jarod42
  • 203,559
  • 14
  • 181
  • 302
CD86
  • 979
  • 10
  • 27

1 Answers1

3

X is a injected class name, so indeed X and X<Features...> are equivalent inside the class.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • I see. thank you. So this is for the very same reasons why I dont need to repeat the template parameters of a class template when defining member functions of this class inside its scope, as opposed to defining them separately outside the class scope. there I have to prepend them before defining a member function. And what about my other question? – CD86 Aug 16 '18 at 07:37
  • That is a *simple* constructor taking base classes to construct them. – Jarod42 Aug 16 '18 at 07:43
  • yes I know, but in this specific case, I dont see any use, since upon construction of X I cannot pass lvalues of base classes to its constructor since those, as template classes, depend on their derived classes ; ) – CD86 Aug 16 '18 at 08:13
  • Since the class does nothing, hard to find a real use case for these constructor, `X{CuriousBase{/*...*/}}` might be useful for more complicated `CuriousBase`. – Jarod42 Aug 16 '18 at 09:25