使用专用可变参数模板的声明 class
using declaration of a specialized variadic template class
是否可以定义 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,强调我的:
When template argument packs or default template-arguments are used, a template-argument list can be
empty. In that case the empty <>
brackets shall still be used as the template-argument-list. [ Example:
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<>;
另外,打字更方便!
是否可以定义 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,强调我的:
When template argument packs or default template-arguments are used, a template-argument list can be empty. In that case the empty
<>
brackets shall still be used as the template-argument-list. [ Example: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<>;
另外,打字更方便!