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

时间:2015-01-23 03:05:44

标签: c++ c++11 variadic-templates using-statement

是否可以定义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;
}

1 个答案:

答案 0 :(得分:4)

不,那是不可能的。从标准§14.3/ 4开始,强调我的:

  

使用模板参数包或默认模板参数时, template-argument 列表可以是   空。在这种情况下,空的<>括号仍将被用作作为 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
     

-end example]

但是,写FloatType<>有什么不对?如果看到空的圆角真的让你烦恼,你可以为它们引入另一个别名,但这种混淆:

using DefFloatType = FloatType<>;

另外,它打字更多!