Suppose I have a class template template <typename T> class X
Is it somehow possible to use type traits or a similar technique to call a (static) method of T but only if the type T declares such method, e.g. something like this:
template <typename T>
class X {
static void foo(){
if(has_method(T,bar)) //Something like this
T::bar(); //If T has no bar() method, then foo does nothing
}
};
Because of the SFINAE rule,
foo_impl#1 is not in the overload set whenU::bar()is not a valid expression, andfoo_impl#2 gets called instead. If type deduction does succeed forfoo_impl#1, it will always be a better conversion than the ellipsis.Ideone demo: http://ideone.com/UKVmIB