I’m sorry this sounds like a common question, I couldn’t find the answer to my problem as far as I looked. The closest post would be this one: Template Specialization for basic POD only
Let’s say I have a class template <class T> class A {...};, and I want to overload operator+ as an internal binary operator (two objects of type A), and as a mixed binary operator (object of type A and numeric POD type).
Ideally, what I would like to write is:
#include <type_traits>
using namespace std;
// Declare/fine template
template <class T> class A {...};
// Internal binary operator
template < class T, class U >
A< typename common_type<T,U>::type >
operator+ ( const A<T> &a, const A<U> &a ) { ... }
// Mixed binary operator
template < class T, class U >
A< typename common_type<T,U>::type >
operator+ ( const A<T> &a, const U &b ) { ... }
But then it seems like the second definition is in conflict with the first one. Using the second definition, I know how to make sure U is a numeric POD type, that’s not the point. If I go that way, the problem is that I have no way of knowing what underlying template type is enclosed in U if it is some A.
Please tell me if my question is not clear enough, and thanks in advance! 🙂
EDIT: The template specification got wiped out by the HTML filter, in my last sentence “U if it is some A<T>“. In short, I’m saying T is hidden.
You can make it work with a little helper trait to distinguish specializations of
Afrom more general types:This excludes the second overload from the viable set immediately, and so the problem of determining the return type of the second overload never comes up when only the first one is desired.
(This is an example of using
enable_ifin defaulted template arguments to control the overload set.)