Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRTP and template template parameters limitation

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?

like image 963
svoltron Avatar asked Sep 20 '18 08:09

svoltron


1 Answers

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.

like image 125
StoryTeller - Unslander Monica Avatar answered Nov 04 '22 09:11

StoryTeller - Unslander Monica