Type conflict in template operator overload
c++, c++11
Solution
You can make it work with a little helper trait to distinguish specializations of `A` from more general types:
#include <type_traits>
// "A" template
template <typename> class A {};
// Traits for "A-ness":
template <typename> struct is_a : std::false_type { };
template <typename T> struct is_a<A<T>> : std::true_type { };
// Operators:
template <class T, class U>
A<typename std::common_type<T, U>::type>
operator+(const A<T> & a, const A<U> & b);
template <class T, class U,
typename = typename std::enable_if<!is_a<U>::value>::type>
A<typename std::common_type<T, U>::type>
operator+(const A<T> & a, const U & b);
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_if` in defaulted template arguments to control the overload set.)
Problem
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.