[英]Out-of-class definition of function of specialized inner class template?
Please consider the following ill-formed program: 请考虑以下不正确的计划:
struct S {
template<class T> struct J { };
};
template<>
struct S::J<void> {
void f();
};
template<>
void S::J<void>::f() {} // ERROR
$ clang++ -std=c++11 test.cpp
no function template matches function template specialization 'f'
$ g++ -std=c++11 test.cpp
template-id ‘f<>’ for ‘void S::J<void>::f()’ does not match any template declaration
Why doesn't the definition of f
compile? 为什么f
的定义没有编译? How do I define the function f
correctly in the above? 如何在上面正确定义函数f
?
The clang error is very helpful here: clang错误在这里非常有用:
no function template matches function template specialization 'f'
// ^^^^^^^^^^^^^^^^^
The syntax you're using is for a function template. 您使用的语法是函数模板。 But f
isn't a function template, it's just a function. 但是f
不是函数模板,它只是一个函数。 To define it, we don't need the template
keyword: 要定义它,我们不需要template
关键字:
void S::J<void>::f() {}
At this point, S::J<void>
is just another class, so this is no different than your standard: 此时, S::J<void>
只是另一个类,所以这与您的标准没有什么不同:
void Class::method() { }
You'd only need template
if you were defining a member function of a template, for instance: 如果要定义template
的成员函数,则只需要模板,例如:
template <typename T>
void S::J<T>::g() { }
or a member function template: 或成员函数模板:
template <typename T>
void S::J<void>::h<T>() { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.