When writing an overloaded function from an rvalue reference and a const reference, you might have code duplication, so I sometimes do both with the same code. As shown here:
#include <iostream>
#include <type_traits>
struct A {
template <typename T>
A& operator=(T&&) {
if constexpr (::std::is_rvalue_reference_v<T&&>) {
::std::cerr << "operator= move\n";
} else {
::std::cerr << "operator= copy\n";
}
return *this;
};
};
Now it is my understanding that this should implement both an A& operator=(T const&)
and an A& operator=(T&&)
. So given this code, in theory I would expect this invocation:
int main() {
A a,b;
a = b;
a = ::std::move(b);
}
to produce the following output:
operator= copy
operator= move
However, to my surprise, the second line (!) is missing. How can I cover both at the same time?
I am compiling with g++ 8.3.0 and -std=c++17
.