partial specialization of function templates
c++, function-templates, template-specialization, templates
Solution
Write a type traits class for your condition:
template<class T>
struct IsIntFloatOrString {
enum { value = boost::is_same<T, int>::value
or boost::is_same<T, float>::value
or boost::is_same<T, string>::value };
};
Use boost::enable_if and disable_if:
template<typename T1>
typename boost::enable_if<IsIntFloatOrString<T1> >::type
func(T1 &t) {
cout << "t1" << endl;
}
template<typename T2>
typename boost::disable_if<IsIntFloatOrString<T2> >::type
func(T2 &t) {
cout << "t2" << endl;
}
Problem
In the below code snippet, ``` template<typename T1> void func(T1& t) { cout << "all" << endl; } template<typename T2> void func(T2 &t) { cout << "float" << endl; } // I do not want this // template<> void func(float &t) int main() { int i; float f; func(i); // should print "all" func(f); // should print "float" return 0; } ``` I would like to have the templates modified which by passing any type other than float will print "all" and passing float will print "float". I do not want template specialization, instead have partial specialization which will act accordingly based on input type. How should i go about it. Thanks in advance. Well the scenario, i'm currently facing is like, I need to have the following defined, ``` template<typename T1> void func(T1 &t) { cout << "t1" << endl; } template<typename T2> void func(T2 &t) { cout << "t2" << endl; } ``` The following calls should print "t2" ``` func(int) // print "t2" func(float) // print "t2" func(string) // print "t2" ``` The following calls should print "t1" ``` func(char) // print "t1" func(xyz) // print "t1" ... func(abc) // print "t1" ``` some kind of grouping like the above where few should call the partial specialization implementation and others should call the default implementation.