#include <iostream>
template <typename T>
struct Wrapper {
operator T const &() const & {
std::cout << "Wrapper::operator T const &() const &\n";
return _obj;
}
operator T&() & {
std::cout << "Wrapper::operator T&() &\n";
return _obj;
}
operator T&&() && {
std::cout << "Wrapper::operator T&&() &&\n";
return std::move(_obj);
}
private:
T _obj;
};
struct Test {
Test& operator=(Test const &test) {
std::cout << "Test& Test::operator=(Test const &)\n";
return *this;
}
Test& operator=(Test &&test) {
std::cout << "Test& Test::operator=(Test &&)\n";
return *this;
}
};
int main() {
Test test;
Wrapper<Test> wrapperTest;
test = wrapperTest; // OK for all
test = std::move(wrapperTest); // OK for GCC and ICC, not for Clang and VC++
return 0;
}
VC++ :
(34): error C2593: 'operator =' is ambiguous
(26): note: could be 'Test &Test::operator =(Test &&)'
(25): note: or 'Test &Test::operator =(const Test &)'
(69): note: while trying to match the argument list '(Test, Wrapper)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Clang :
:34:7: error: use of overloaded operator '=' is ambiguous (with operand types 'Test' and 'typename std::remove_reference &>::type' (aka 'Wrapper'))
test = std::move(wrapperTest); // OK for GCC and ICC, not for Clang and Microsoft Visual C++
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~
:25:8: note: candidate function
Test& operator=(Test const &test) { std::cout << "Test& Test::operator=(Test const &)\n"; return *this; }
^
:26:8: note: candidate function
Test& operator=(Test &&test) { std::cout << "Test& Test::operator=(Test &&)\n"; return *this; }
^
1 error generated.
g++? You provided forVC++, but your question's title saysg++. Which is it? Visual Studio C++ is a different compiler than GNU g++.operator=()is ambiguous. How is that different?&and&&function specifiers do? First time I'm seeing them