#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++:Перегруженная двумерность оператора на Clang, но не на GCC, какая из них правильная?
(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.
Где ваш пример для '' г ++? Вы предоставили для 'VC++', но название вашего вопроса говорит 'g ++'. Что он? Visual Studio C++ - это другой компилятор, чем GNU g ++. –
Я в замешательстве. Они оба упоминают одну и ту же проблему: 'operator =()' неоднозначно. Как все иначе? –
@ThomasMatthews Я сказал, что этот код компилируется на GCC, но не на Clang и VC++, я помещаю сообщение об ошибке Clang и VC++. – Johnmph