c++ - Function overloads and inherited types are not calling the correct overload -
given following base class:
struct valuetype { string tostring(string const& format) const; }; i want overload called types deriving valuetype:
string formatvalue(const valuetype& value, const string& format) { return value.tostring(format); } otherwise, want overload called:
template <typename t> string formatvalue(const t& value, const string& format); how can ensure derived types not instead call second overload?
i not fond of trying different reasons (including valuetype interface, why not use anytostring always?), @ rate should able solve problem sfinae
template <typename t> typename enable_if< !is_base_of<valuetype, t>::value, string>::type formatvalue( t const & value, const string& format ) { ... } what code (once make compile :) inhibiting template function when condition met. when compiler considers template overload try substitute type, , enable_if instantiation fail have nested type if condition met, substitution fails , template discarded. after that, best overload version takes valuetype object.
Comments
Post a Comment