提问人:TStancek 提问时间:10/31/2023 最后编辑:HolyBlackCatTStancek 更新时间:11/2/2023 访问量:115
模板成员函数不能显式专用
Template member function cannot be explicitly specialized
问:
我在类中有一个函数的声明
class A {
template<typename T, typename... ARGS>
void Func( ARGS&&... args) {
//DoSomeWork
}
};
然后尝试将其专门用于此类类型
template<>
void A::Func<int>(bool arg) {
//DoSomeOtherWork
}
我收到错误
错误 C2910:“A::Func”:无法显式专用
有没有办法使模板专用化,或者我必须找到解决方法?如果没有办法对模板进行专业化,为什么会这样呢?
答:
1赞
HolyBlackCat
10/31/2023
#1
专用化无法更改参数类型。如果(主模板)通过引用接受参数,则专用化也必须这样做,例如:Func
template<>
void A::Func<int>(bool &&arg) {}
在这种情况下,MSVC 也无济于事。Clang 做得更好:
<source>:9:9: error: no function template matches function template specialization 'Func'
9 | void A::Func<int>(bool arg) {
| ^
<source>:3:8: note: candidate template ignored: could not match 'ARGS &&' against 'bool'
3 | void Func( ARGS&&... args) {
| ^
评论
error: no function template matches function template specialization 'Func'
note: candidate template ignored: could not match 'ARGS &&' against 'bool'
bool
bool &&