You can limit T , but using "Substitution error is not an error" (SFINAE):
template <typename T> typename std::enable_if<std::is_base_of<bar, T>::value>::type foo() { }
If T not derived from bar , the specialization of the function template will fail and will not be taken into account when resolving overloads. std::enable_if and std::is_base_of are the new components of the C ++ standard library added to the upcoming revision, C ++ 0x. If your compiler / standard library implementation does not yet support them, you can also find them in C ++ TR1 or Boost.TypeTraits.
source share