Saturday, July 14, 2018

c++ - Overloaded operator ambiguity on Clang but not on GCC, which one is correct?

#include 


template
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 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.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...