Value category of defaulted assignment operator - lvalue or rvalue? (clang vs gcc)
Asked Answered
T

0

8

Consider the following code:

struct S 
{ 
    S& operator=(const S&) = default;
};

int main()
{
    &(S() = S());
}

GCC (trunk) complains with:

  error: taking address of rvalue [-fpermissive]
    8 |     &(S() = S());
      |      ~~~~~^~~~~~

I found the above error surprising, as I would expect an S& return type to produce an lvalue expression. Clang (trunk), on the other hand, accepts the code without any error or warning.

live on godbolt.org

  • What compiler is correct here?

  • Is S() = S() an rvalue or an lvalue?

Tit answered 19/9, 2020 at 0:39 Comment(3)
interestingly, older GCCs use the error message "taking the address of a temporary", rather than "an rvalue". Perhaps this hints at what it's doing -- the assignment operator returns the LHS, which is a temporary, which you're then trying to take the address of?Dissection
gcc.gnu.org/bugzilla/show_bug.cgi?id=59950Toxicant
^^ is correct, this was a gcc bug. Fixed for 9.5/10.4/11.3/12.1 and later releases.Disorderly

© 2022 - 2024 — McMap. All rights reserved.