Is there a way to impose a restriction on what X can be? E.g. I might want myfunc1 to accept only double or int, or I might want myfunc2 to return only double* or int*.
If the bodies of the code will be similar for different types, but you only want specific types to be available, then I would have overloaded functions that call a hidden/private template member function. For example:
1 2 3 4 5 6 7 8 9 10 11 12
class MyClass
{
private:
template <typename T> void myfunc1internal(T foo)
{
// do the actual work here
}
public:
void myfunc1(int foo) { myfunc1internal(foo); }
void myfunc1(double foo) { myfunc1internal(foo); }
};
In general it is not possible in C++, unless you wish to create an overloaded function for every type parameter you plan to support, just as Disch showed. It would be possible if concepts were accepted in C++0x, but we will have to wait for them for the next 5 years or more.