简体   繁体   English

专门的内部类模板的功能的外层定义?

[英]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.

相关问题 模板 class 的模板成员 function 的类外定义的语法 - Syntax of out-of-class definition of a template member function of a template class 模板类中的 C++20 类外定义 - C++20 out-of-class definition in a template class 专用类模板的类构造函数定义 - Out of class constructor definition for a specialized class template 具有类类型非类型模板参数的类模板成员的类外定义 - Out-of-class definition of member of a class template with a class-type non-type template parameter 不可能在 function 定义的类外声明符中完全限定类名 - Impossible to fully qualify class-name in out-of-class declarator of function definition 为什么类外成员模板定义需要重复其声明“requires-clause” - Why does an out-of-class member template definition need a repetition of its declaration 'requires-clause' 模板类中专用功能的声明 - Declaration of specialized function in template class 专门课程的专业功能 - specialized function of a specialized class 静态constexpr类成员何时需要类外定义? - When does a static constexpr class member need an out-of-class definition? 当模板类不专用时,成员函数专用模板的解决方法 - Workaround for specialized template for member function, when template class is not specialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM