【问题标题】:angle brackets after struct结构后的尖括号
【发布时间】:2013-09-05 13:02:21
【问题描述】:

我在理解 C++ 代码中的以下行时遇到问题:

template<class Variable> struct strVar< :: namespaceName::strVar2_<Variable>> : public trueType {};

struct strVar 后面的尖括号是什么意思?我以前从未听说过这种风格。

该行不是用我的编译器编译的,而是来自一个正在运行的软件,所以在某种意义上它一定是对的。

【问题讨论】:

  • 这是类模板strVar的部分特化。这应该包含在任何good C++ book 中。
  • 可能是一个模板专业化,虽然我不知道这个会如何工作。

标签: c++ struct class-template


【解决方案1】:

代码定义了类模板strVar 的部分特化。在代码前面的某个地方,必须至少有一个主模板的声明:

template <class T> struct strVar;

然后,您发布的代码提供了一个定义,只要它的模板参数(对应于T)恰好是类模板::namespaceName::strVar2_ 的特化,就会用于strVar

例子:

strVar<int> x;  // will use primary template
strVar<::namespaceName::strVar2_<int>> x;  // will use the specialisation

【讨论】:

  • 好的,谢谢!我会试着弄清楚......也许我忘了包括其他一些标题。我从未听说过结构的部分特化,但你是对的,它一定是这样的
猜你喜欢
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
相关资源
最近更新 更多