I’m trying to specialize just one method of a class template.
The accepted answer there definitely does work, but what does this code mean to the compiler and why doesn’t it work as expected?
#include <stdio.h>
template <typename T>
struct Node
{
void split() { puts( "Default method" ) ; }
// this compiles, but it doesn't appear to do anything!
template <int> void split() { puts( "Int method" ) ; }
} ;
// This definitely works
//template <> void Node<int>::split() { puts( "Int method" ) ; }
int main()
{
Node<double> n ;
n.split() ; // "Default method"
Node<int> i;
i.split(); // "Default method" again!
}
Call like this:
If you intend to call the function template. Note that the function template is not a specialization. It rather is a primary function template, and to call it, you’ve to provide the template argument explicitly.
You can do even this:
But this is not allowed: