Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary expression assigned by rvalue doesn't use move asssignment operator

Tags:

c++

gcc

c++11

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.

like image 560
Kan Li Avatar asked Oct 13 '13 04:10

Kan Li


1 Answers

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.

like image 116
Ali Avatar answered Nov 13 '22 00:11

Ali