【问题标题】:CRTP and template template parameters limitationCRTP 和模板模板参数限制
【发布时间】:2018-09-20 08:54:45
【问题描述】:

我正在尝试使用 CRTP,但我对为什么以下代码无法编译感到困惑。

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
  };

你知道在CRTP的情况下模板模板参数是否有限制吗?

【问题讨论】:

  • 好问题。不过,ComponentX 定义之前的所有内容都与 MCVE 无关。
  • 谢谢,是的,你完全正确。只是为了举例说明如何使用它(我忘了写最后一部分)。

标签: c++ templates template-meta-programming crtp


【解决方案1】:

类模板名称仅在类模板定义的开头{ 之后,在其范围内代表“当前特化”(即,它是注入的类名称)。在此之前,它是一个模板名称。

所以CBase&lt;ComponentX&gt; 尝试将模板作为参数传递给CBase,它需要一组类型。

修复相当简单:

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

ComponentX&lt;CBase&gt; 是您希望作为类型参数提供的专业化名称。

【讨论】:

  • 谢谢,这完全有道理
猜你喜欢
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 2012-06-30
  • 2013-07-15
相关资源
最近更新 更多