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

Yantao Xie

The following code can be build successfully.

#include <iostream>
#include <string>
using namespace std;

template <class T>
struct Foo
{
    template <class S>
    void print(const T& t, const S& s);
};

template <>
struct Foo<int>
{
    template <class S>
    void print(const int& t, const S& s)
    {
        cout << s << " " << t << endl;
    }
};

int main(void)
{
    string str("hello");
    Foo<int> foo;
    foo.print(7, str);
    return 0;
}

But if I move the definition of the member function Foo<int>::print(...) outside the definition of the class Foo<int> like below

template <>
template <class S>
void Foo<int>::print(const int& t, const S& s)
{
    cout << s << " " << t << endl;
}

I got a GCC compiling error like this:

error: too many template-parameter-lists
 void Foo<int>::print(const int& t, const S& s)
      ^

Where did I make a mistake?

T.C.

§14.7.3 [temp.expl.spec]/p5:

Members of an explicitly specialized class template are defined in the same manner as members of normal classes, and not using the template<> syntax. The same is true when defining a member of an explicitly specialized member class. However, template<> is used in defining a member of an explicitly specialized member class template that is specialized as a class template.

print is not a class template. Hence, remove the template <>:

template <>
struct Foo<int>
{
    template <class S>
    void print(const int& t, const S& s);
};

template <class S>
void Foo<int>::print(const int& t, const S& s)
{
    cout << s << " " << t << endl;
}

Demo.


Note that if you do not explicitly specialize Foo<int>, but tries to define Foo<int>::print directly, then you are explicitly specializing a member of a particular implicit instantiation of Foo, and not defining a member of an explicit specialization. For that, you need template <>:

template <class T>
struct Foo
{
    template <class S>
    void print(const T& t, const S& s);
};

template <>
template <class S>
void Foo<int>::print(const int& t, const S& s)
{
    cout << s << " " << t << endl;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to define member function of partially specialized template class using boost::enable_if

From Dev

C++ - Define member function outside template-class but in header

From Dev

In template class member function body outside class definition, when are template parameters required?

From Dev

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

From Dev

Can I define a private template function outside of a class?

From Dev

How to define a constructor template of a class template outside of the class template?

From Dev

How to define a constructor template of a class template outside of the class template?

From Dev

outside definition of member class of an explicitly specialized member class

From Dev

Define operator[]() (array subscription) for template class outside of class definition

From Dev

Using a partial template class as a specialized template function

From Dev

How to define a template class function with nested type of the template argument in its signature outside the class

From Dev

How to define a member function outside class in python?

From Dev

how do I specialize a bound template friend function to a template class?

From Dev

How do I pass arbitrary member function of ANY class to template in order to resolve its signature?

From Dev

How can I specialize a template class member function?

From Dev

How can I Declare/define/initialize a static member variable of template classes as static member variables of a class?

From Dev

Partial Template specialization definition outside of class definition

From Dev

Partial Template specialization definition outside of class definition

From Dev

C++ member function definition class prefix shortcut (also template)

From Dev

C++ member function definition class prefix shortcut (also template)

From Dev

How do I access a template class member field in a specialised template class in C++?

From Dev

How to define a template of template class

From Dev

Specialized constructors for a template class

From Dev

how to pass pointer to member function of a template class?

From Dev

How to define a static member of a nested template (template class inside template class)

From Dev

inner class method definition outside of a template class

From Dev

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

From Dev

can I use SFINAE to selectively define a member variable in a template class?

Related Related

  1. 1

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

  2. 2

    How to define member function of partially specialized template class using boost::enable_if

  3. 3

    C++ - Define member function outside template-class but in header

  4. 4

    In template class member function body outside class definition, when are template parameters required?

  5. 5

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

  6. 6

    Can I define a private template function outside of a class?

  7. 7

    How to define a constructor template of a class template outside of the class template?

  8. 8

    How to define a constructor template of a class template outside of the class template?

  9. 9

    outside definition of member class of an explicitly specialized member class

  10. 10

    Define operator[]() (array subscription) for template class outside of class definition

  11. 11

    Using a partial template class as a specialized template function

  12. 12

    How to define a template class function with nested type of the template argument in its signature outside the class

  13. 13

    How to define a member function outside class in python?

  14. 14

    how do I specialize a bound template friend function to a template class?

  15. 15

    How do I pass arbitrary member function of ANY class to template in order to resolve its signature?

  16. 16

    How can I specialize a template class member function?

  17. 17

    How can I Declare/define/initialize a static member variable of template classes as static member variables of a class?

  18. 18

    Partial Template specialization definition outside of class definition

  19. 19

    Partial Template specialization definition outside of class definition

  20. 20

    C++ member function definition class prefix shortcut (also template)

  21. 21

    C++ member function definition class prefix shortcut (also template)

  22. 22

    How do I access a template class member field in a specialised template class in C++?

  23. 23

    How to define a template of template class

  24. 24

    Specialized constructors for a template class

  25. 25

    how to pass pointer to member function of a template class?

  26. 26

    How to define a static member of a nested template (template class inside template class)

  27. 27

    inner class method definition outside of a template class

  28. 28

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

  29. 29

    can I use SFINAE to selectively define a member variable in a template class?

HotTag

Archive