Anyone knows why the following code doesn't compile:
[hidden]$ g++ -v |& tail -n 1
gcc version 4.8.1 20130603 (Red Hat 4.8.1-1) (GCC)
[hidden]$ cat c.cpp
struct X {
X() = default;
X(const X&) = default;
X(X&&) = default;
X& operator=(const X&) = delete;
X& operator=(X&&) = default;
};
void f(bool t) {
X a, b;
(t ? a : b) = X();
}
[hidden]$ g++ -std=c++11 -c c.cpp
c.cpp: In function ‘void f(bool)’:
c.cpp:11:15: error: use of deleted function ‘X& X::operator=(const X&)’
(t ? a : b) = X();
^
c.cpp:5:6: error: declared here
X& operator=(const X&) = delete;
^
c.cpp:11:15: error: use of deleted function ‘X& X::operator=(const X&)’
(t ? a : b) = X();
^
c.cpp:5:6: error: declared here
X& operator=(const X&) = delete;
^
Isn't that X()
rvalue so the move assignment operator should be called in this case? What section in C++11 standard talks about the case ternary expression assigned by rvalue?
Note: the ternary expression is lvalue in this case because if I change the = delete
to = default
, it compiles.
This a bug in the compiler, see Bogus overload resolution for the assignment operator in assignment to a conditional, it still fails in gcc 4.9.
The problem is not with the ternary operator but that the overload resolution ends up at the wrong assignment operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With