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 :
t
Adding this main() function:
int main()
{
int x = 1, y = 2;
foo( x, y );
}
This is what the compiler error is:
main.cpp: In function int main(): main.cpp:15:15: error: no matching
function for call to foo(int&, int&)
foo( x, y );
^ main.cpp:15:15: note: candidate is:
main.cpp:7:6: note: template void foo(T, T, ...) void
foo(T a, T b, ...)
^ main.cpp:7:6: note: template argument deduction/substitution failed:
main.cpp:6:60: error:
using xvalue (rvalue reference) as lvalue
typename CheckTplusT = decltype(std::declval() += std::declval())>
The key line is using xvalue (rvalue reference) as lvalue
This is the documentation for declval
This workaround works for me:
template() += *std::declval())>
void foo(T &a, T b, ...)
{
a += b;
}
int main()
{
int a = 1, b = 2;
foo( a, b );
std::cout << a << std::endl;
}
outputs 3
You can also use declval
of course.