【发布时间】:2018-12-03 04:26:08
【问题描述】:
我的问题很基本。我试图给自己一个方便的类模板实例化,它在数学上做一些直观的事情。我希望能够通过将其实例化为Q<31> 来实例化一个名为Q<0,31> 的类。根据cppreference.com,这个should be possible:
template<class T>
class A<int, T*, 5> {}; // #3: partial specialization where T1 is int, I is 5,
// and T2 is a pointer
但是当我尝试这个时:
template< unsigned integral, unsigned fractional >
class Q: public Signed<integral + fractional + 1u> {};
// There's a shorthand for Q notation where you don't specify the number of integer bits, and it's assumed to be 0
template< unsigned fractional > class Q<0u, fractional>: public Signed<fractional + 1> {};
static_assert( std::is_same< Q<31>, Q<0,31> >::value, "Partial specialization should be equivalent to no integer component");
然后我收到一条错误消息,提示我没有传递足够多的模板参数
【问题讨论】:
-
似乎有很多不必要的代码来迷惑读者。只要把你所拥有的和你想要的。
Q<31>不能自然地解释为Q<0,31>,因为默认参数从左侧开始。 -
您似乎误解了链接。部分特化不会改变您指定模板参数的方式,它会在提供的参数与部分特化匹配时替换实例化类的定义。
标签: c++ templates partial-specialization class-template