How to define a method with a generic parameter
Hello everyone !
I think i've solved the problem.
like this :
Code:
template <class myType>
void TestEmpty(myType object);
I want to implement my own exception class, and i need to define a method with a generic parameter.
something like this:
Code:
void Test_Empty(T object) {
if (obj==NULL) /* the object type is unknown at compilation time*/
cout<< "No data exception";
}
The problem is that I do not know how to define a default type parameter of my test function.
Once it can be of type int, other wise of type float for instance.
Can you please give me some suggestions.
Re: How to define a method with a generic parameter
Templated functions cannot have default type arguments, however it is easily worked around by making your function a static member of a templated empty class which can have default type arguments.
Code:
template < typename T = int >
struct FuncHolder
{
static void TestEmpty( T obj )
{
// .....
}
};
At least i think thats what you meant.
Re: How to define a method with a generic parameter
Quote:
Originally Posted by
munteanu24d
The problem is that I do not know how to define a default type parameter of my test function.
Once it can be of type int, other wise of type float for instance.
Can you please give me some suggestions.
Default types do not make sense for functions. Your function will be called with an argument and the argument will definitely have a type, thus the default type will never come into play.
However it is not clear what you are trying to do. If you want your function to be called with int and float types you should not compare the argument with null. Comparing an int with null make little sense and comparing a float with null makes no sense.
So maybe you explain how you intend to use your function.