使用专门的可变参数模板类的声明

用户名

是否可以以FloatType这样的方式定义,我可以声明f1

FloatType f1;

代替

FloatType<> f1;

如果我尝试使用前者,我会得到一个

error: use of class template 'FloatType' requires template arguments

template <typename T, typename... Args>
class Type
{
};

template <typename... Args>
class Type<float, Args...>
{
};

template <typename... Args>
using FloatType = Type<float, Args...>;

int
main(int, char **)
{
    FloatType<> f1;
    FloatType<float> f2;

    return 0;
}
巴里

不,那是不可能的。根据标准§14.3/ 4,重点是:

使用模板参数包或默认模板参数时模板参数列表可以为空。在那种情况下,空<>括号仍应用template-argument-list[ 例子:

  template <class T = char> class String;
  String<>* p; // OK: String<char>
  String* q;   // syntax error

  template <class ... Elements> class Tuple;
  Tuple<>* t; // OK: Elements is empty
  Tuple* u;   // syntax error

—结束示例]

但是,那写作有什么问题FloatType<>呢?如果看到空括号确实让您感到讨厌,则可以为它们引入另一个别名,但是这种混淆的东西:

using DefFloatType = FloatType<>;

此外,还有更多输入内容!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章