Search code examples
crvaluelvaluecompound-literals

Are compound literals always lvalue?


I was reading C Notes for Professionals, in which, it claims that Compound Literals can only be lvalues. But LLMs have mixed answers upon that stating it could depend upon the context and could be rvalues in certain situations.

When I used godbolt.org, I found out that scalar compound literals get optimized away as literals by the compiler. For instance, (int) {47} is same as 47 when compiled to assembly; While the former is an lvaue and the latter is an rvalue. I referenced C23 Standard, N3220.pdf, I was surprised to learn that the standard doesn't use the term rvalue (it uses non-lvalue); My whole life was lie. Few lvalues like (register int) {47} cannot be addressed at all and yet it is an lvalue. I just realised my understanding of lvalue isn't what I think, could someone please explain it? And also, are Compound Literals always lvalues?


Solution

  • Forget all the books and LLM hallucinations and go straight to the spec (final draft N3096):

    6.5.2.5 p6: The value of a compound literal is that of an lvalue corresponding to the unnamed object.

    So a clear yes, it is an lvalue.