I'm trying to eliminate an overload from an overload set if operator+= is missing.
I know how to check if T+T is legal :
template<typename T,
typename CheckTplusT = decltype(std::declval<T>() + std::declval<T>())>
void foo(T a, T b, ...)
{
a = a + b;
}
but this doesn't work for +=
template<typename T,
typename CheckTplusT = decltype(std::declval<T>() += std::declval<T>())>
void foo(T a, T b, ...)
{
a += b;
}
Is this fixable by using another expression inside decltype or do I need another SFINAE construct?
The reason I need this eliminated from the overload set is that it clashes with another overload that accepts a functor to be used as an alternative to +=. Compilers are VS2013, gcc4.8
foodoes not work?std::declval<T>(). But you can call + on rvalues. Compare 2+2 and 2+=2+=is a method, you can call it on rvalues (unless the method has a&qualifier). The problem is with built-in+=and fundamental types.std::complex<>.enable_ifif you have Boost available. Boost.TypeTraits'boost::has_plus_assigncould work. This might be the preferred non-C++11 option.