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?

the original question located here.

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

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -