【发布时间】:2017-11-25 15:01:34
【问题描述】:
我有一个类模板 Foo:
template <class A, A value, class B>
class Foo {};
我有一个函数模板 validateType()
template <class T>
bool validateType() {
return false;
}
现在我想将它专门用于某些类型,包括 Foo,以便该函数在编译时执行一些 static_asserts。我试过这个:
template <class A, class B, Foo<A, A val, B>>
bool validateType() {
// do some static asserts
}
还有这个:
template <class A, A val, class B>
bool validateType<Foo<A, val, B>>() {
// do some static asserts
}
在第一个中,编译器说:
error: wrong number of template arguments (2, should be 3)
template <class A, class B, Foo<A, A val, B>>
^~
note: provided for ‘template<class A, A value, class B> class Foo’
class Foo {};
^~~
error: two or more data types in declaration of ‘validateType’
bool validateType() {
^
error: expected ‘>’ before ‘{’ token
bool validateType() {
^
在第二种情况下,我得到了
error: non-class, non-variable partial specialization ‘validateType<Foo<A, val, B> >’ is not allowed
bool validateType<Foo<A, val, B>>() {
^
这应该怎么做?
【问题讨论】:
标签: c++ templates template-specialization specialization