Overloading a static function in a template class

Hi,
I have a template class, parametrized by types K and V. I want to overload a static function in this template. I've tried like this :

template < int, typename V >
static int hashU(int s) {

return s ;
}

template < float, typename V >
static int hashU(float s) {

return floor(s) ;
}

But it doesn't compile well, I have the following error :

hashT.tpp:60:12: erreur: ‘float’ is not a valid type for a template non-type parameter

How can I solve it ? Thanks for your help :)


Can't figure out exactly what you need given your example code.

Here's a quick example of a templated class with two types and some overloaded static functions. Perhaps the syntax might help somewhat. :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

template <class T, class U > class MyClass
{
  T data;
  U otherData;
public:
  static void DoStuff( int i );
  static void DoStuff( float f );
};

template <class T, class U> void MyClass<T,U>::DoStuff( int i )
{
  std::cout << "int version: " << i << std::endl;
}

template <class T, class U> void MyClass<T,U>::DoStuff( float f )
{
  std::cout << "float version: " << f << std::endl;
}

int main( int argc, char* argv[] )
{
  MyClass<int,float>::DoStuff( 1 );     // DoStuff( int )
  MyClass<int,float>::DoStuff( 15.5f ); // DoStuff( float )
}
This syntax works pretty well, thank you so much :D
Topic archived. No new replies allowed.