If I have a template class:

template <class T>
class myclass
{
public:
  T function();
}

and it has a member function for example T function(),
it is possible to define it separately for different kind of template parameters?

So define somehow:

int myclass::function();
double myclass::function();
mystruct myclass::function();

with totally different kind of code?

You can specialize myclass::function based on the template parameters of myclass:

#include <iostream>

struct mystruct {};

template <class T>
class myclass
{
public:
  T function()
  {
    std::cout<<"Generic template\n";
    return T();
  }
};

template<>
int myclass<int>::function()
{
  std::cout<<"int specialization\n";
  return int();
}

template<>
double myclass<double>::function()
{
  std::cout<<"double specialization\n";
  return double();
}

template<>
mystruct myclass<mystruct>::function()
{
  std::cout<<"mystruct specialization\n";
  return mystruct();
}

int main()
{
  myclass<int> a;
  myclass<double> b;
  myclass<mystruct> c;
  myclass<char> d;

  a.function();
  b.function();
  c.function();
  d.function();
}

Thank you very much!
thats what I need exactly

Edited 6 Years Ago by merse: n/a

Well there are also other ways.

Here is a another way :

#include<iostream>

using namespace std;
 
template<typename Type>
struct assert_numeric{ const static int i = 0; };

template<> struct assert_numeric<short>					{ const static int i = 1;	};
template<> struct assert_numeric<unsigned short>		{ const static int i = 1;	};
template<> struct assert_numeric<int>					{ const static int i = 1;	};
template<> struct assert_numeric<unsigned int>			{const static int i = 1;	};
template<> struct assert_numeric<float>					{ const static int i = 1;	};
template<> struct assert_numeric<double>				{ const static int i = 1;	};

template<typename Type>
class Numeric{

public:		
	 struct BadType : std::exception{
		BadType(const char *msg) : exception(msg){}
	};
public:
	Numeric()
	{ 
		//int asserter[assert_numeric<Type>::i];// Way# 1 cannot have an array of size 0
		
		if(assert_numeric<Type>::i == 0) //Way number 2 : throw custom exception
			throw BadType("Type is not numerical!");
	}
	~Numeric(){ }
};

int main()
{

	try{
		Numeric<char> j;
	}
	catch(Numeric<char>::BadType& e){ cout << e.what() <<endl; }
	

 
  return 0;
}

Well there are also other ways.

Here is a another way :
<snip>

Did you post this to the correct thread? I don't see how that relates to anything that the OP wanted to do; they wanted to change the behaviour of a template class depending on its type.

There are actually much better ways to do that than a runtime exception - you can write "static" assertions which will generate a compiler error message instead of letting your program wait until runtime; There is a boost library called 'static_assert' which lets you do this with meaningful error messages. (static_assert will also probably become a new keyword in C++0x too)

Boy that went on the wrong thread. Thats it. I need some sleep. And the static assert is also shown in the example.

I think in the other post of Narue, it talks static_assert.

Edited 6 Years Ago by firstPerson: n/a

You can specialize myclass::function based on the template parameters of myclass:

template<>
double myclass<double>::function()
{
  std::cout<<"double specialization\n";
  return double();
}

Dear Narue, I cannot use default parameters?
This kind of construction does not work for me:

template<>
double myclass<double>::function(double x=0.)
{
  ...
}

I get message:
default argument specified in explicit pecialization!

This question has already been answered. Start a new discussion instead.