Suppose you have a family of type-unrelated classes implementing a common concept by means of a given method returning a value:
class A { public: int val() const { ... } };
class B { public: int val() const { ... } };
suppose you need a generic free function taking a T
returning a conventional value for whatever type NOT implementing the val
method or calling the val
method for ALL the types that has one:
template<class T> int val_of(const T& t) { return 0; }
template<class T> int val_of(const T& t) { return t.val(); }
Consider that A and B are just samples: you don't know how many types will ever exist implementing val
, and how many types will exist not implementing it (hence explicit specialization won't scale).
Is there a simple way, based on the C++ standards, to come to a way to statically select the val_of
version?
I was thinking to a std::conditional
or std::enable_if
, but I didn't find a simple way to express the condition.