C++ creating generic template function specialisations
- by Fire Lancer
I know how to specialise a template function, however what I want to do here is specialise a function for all types which have a given method, eg:
template<typename T> void foo(){...}
template<typename T, if_exists(T::bar)>void foo(){...}//always use this one if the method T::bar exists
T::bar in my classes is static and has different return types.
I tried doing this by having an empty base class ("class HasBar{};") for my classes to derive from and using boost::enable_if with boost::is_base_of on my "specialised" version. However the problem then is that for classes that do have bar, the compiler cant resolve which one to use :(.
template<typename T>
typename boost::enable_if<boost::is_base_of(HasBar, T>, void>::type f()
{...}
I know that I could use boost::disable_if on the "normal" version, however I do not control the normal version (its provided by a third party library and its expected for specialisations to be made, I just don't really want to make explicit specialisations for my 20 or so classes), nor do I have that much control over the code using these functions, just the classes implementing T::bar and the function that uses it.
Is there some way to tell the compiler to "always use this version if possible no matter what" without altering the other versions?