I have a template where there are some functions which are correct only for some of the data types and not for all. So, I want to make them available only for those data types and not for all in general. How do I do that?
Can't do it with templates alone, however you can make a series of inline functions that call the template.
1 2 3 4 5 6 7 8 9 10
// assuming your function works with int,long,short but not float/double:
inlinevoid func(int v) { func2(v); }
inlinevoid func(long v) { func2(v); }
inlinevoid func(short v) { func2(v); }
// make this protected/private to prevent it being called with undesired types
template <typename T> void func2(T v)
{
// do whatever
}
I think you can using templates.
You seperate the template into a header and a cpp file and do explicit instantiations of the
required functions in the cpp file.
Include the header as normal, and compile the cpp file. Any types for which there is no
instantiation in the cpp file will cause a linker error.