Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the assignment expression a lvalue reference?

Tags:

c++

For example:

template <typename T> void g(T &&val);
int i = 0; const int ci = i;
g(i = ci);

What is the template argument of g?

like image 827
acgtyrant Avatar asked Jul 03 '26 10:07

acgtyrant


1 Answers

As per §5.18/1:

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand. [...]

So, in

g(i = ci)

the left operand, i, is returned and therefore T is deduced to int&.

You can check this via this snippet:

#include <type_traits>

template <typename T> 
void g(T &&val) {
    static_assert(std::is_same<T, int&>::value, "Nope");
}

int main() {
    int i = 0; const int ci = i;
    g(i = ci);
}

Live demo

like image 122
Shoe Avatar answered Jul 04 '26 23:07

Shoe