CRTP and template template parameters limitation
Asked Answered
M

1

7

I'm trying to experiment with CRTP but I am puzzled on why the following code does not compile.

template<template<class...> class CBase>
struct ComponentX : public CBase<ComponentX>
  {
  // This does NOT compile
  };

template<template<class...> class CBase>
struct ComponentY : public CBase<int>
  {
  // This does compile
  };

Do you know if there is some limitation for template template parameters in the case of CRTP?

Mahound answered 20/9, 2018 at 8:54 Comment(2)
Nice question. Though, everything before ComponentX's definition is irrelevant to the MCVE.Jespersen
Thanks, yes you're totally right. It was just to give an example of how to use it (I forgot to write the last part though).Mahound
J
8

A class template name stands for the "current specialization" (i.e. it is an injected class name) only after the opening { of the class template definition, inside its scope. Before that, it's a template name.

So CBase<ComponentX> is an attempt to pass a template as an argument to CBase, which expects a pack of types.

The fix is fairly simple:

template<template<class...> class CBase>
struct ComponentX : public CBase<ComponentX<CBase>> // Specify the arguments
  {
  // This should compile now
  }; 

ComponentX<CBase> is the name of the specialization you wish to provide as a type argument.

Jespersen answered 20/9, 2018 at 8:59 Comment(1)
Thank you, it totally makes senseMahound

© 2022 - 2024 — McMap. All rights reserved.