Out-of-class definition of function of specialized inner class template?

Andrew Tomazos

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? How do I define the function f correctly in the above?

Barry

The clang error is very helpful here:

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. To define it, we don't need the template keyword:

void S::J<void>::f() {}

At this point, S::J<void> is just another class, so this is no different than your standard:

void Class::method() { }

You'd only need template if you were defining a member function of a template, for instance:

template <typename T>
void S::J<T>::g() { }

or a member function template:

template <typename T>
void S::J<void>::h<T>() { }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using a partial template class as a specialized template function

From Dev

How do I define a template member function outside of a full specialized template class's definition?

From Dev

How do I define a template member function outside of a full specialized template class's definition?

From Dev

inner class method definition outside of a template class

From Dev

Specialized constructors for a template class

From Dev

Passing a derived class to a template function specialized with base class

From Dev

Passing a derived class to a template function specialized with base class

From Dev

Explicit specialization of a function template for a fully specialized class template

From Java

C++20 out-of-class definition in a template class

From Dev

C++20 out-of-class definition in a template class

From Dev

Template specialization within template specialized class

From Dev

Template class with template method specialized for itself

From Dev

Use generalized template class in specialized template functions

From Dev

Use generalized template class in specialized template functions

From Dev

Template class with template method specialized for itself

From Dev

Wrong definition of function in template inherited class

From Dev

Friend template function in-class definition

From Dev

Calling template function from inner class

From Dev

why Specialized template class need forward declaration?

From Dev

template class with a single method specialized in C++

From Dev

using declaration of a specialized variadic template class

From Dev

using declaration of a specialized variadic template class

From Dev

Defining conversion operator for specialized template class only

From Dev

Disable construction of non fully specialized template class

From Dev

outside definition of member class of an explicitly specialized member class

From Dev

How to write virtual function in inner class of template class?

From Dev

specialize template with inner class template of a class template

From Dev

Function pointer definition inside a class template based on the template variable arguments

From Dev

C++ template static integer constants: out of class definition

Related Related

  1. 1

    Using a partial template class as a specialized template function

  2. 2

    How do I define a template member function outside of a full specialized template class's definition?

  3. 3

    How do I define a template member function outside of a full specialized template class's definition?

  4. 4

    inner class method definition outside of a template class

  5. 5

    Specialized constructors for a template class

  6. 6

    Passing a derived class to a template function specialized with base class

  7. 7

    Passing a derived class to a template function specialized with base class

  8. 8

    Explicit specialization of a function template for a fully specialized class template

  9. 9

    C++20 out-of-class definition in a template class

  10. 10

    C++20 out-of-class definition in a template class

  11. 11

    Template specialization within template specialized class

  12. 12

    Template class with template method specialized for itself

  13. 13

    Use generalized template class in specialized template functions

  14. 14

    Use generalized template class in specialized template functions

  15. 15

    Template class with template method specialized for itself

  16. 16

    Wrong definition of function in template inherited class

  17. 17

    Friend template function in-class definition

  18. 18

    Calling template function from inner class

  19. 19

    why Specialized template class need forward declaration?

  20. 20

    template class with a single method specialized in C++

  21. 21

    using declaration of a specialized variadic template class

  22. 22

    using declaration of a specialized variadic template class

  23. 23

    Defining conversion operator for specialized template class only

  24. 24

    Disable construction of non fully specialized template class

  25. 25

    outside definition of member class of an explicitly specialized member class

  26. 26

    How to write virtual function in inner class of template class?

  27. 27

    specialize template with inner class template of a class template

  28. 28

    Function pointer definition inside a class template based on the template variable arguments

  29. 29

    C++ template static integer constants: out of class definition

HotTag

Archive