Follow

c++ 

It's annoying how arbitrary the rules for value categories are.

  • casting a value as an rvalue reference is an xvalue since it is an addressable result available to the programmer but the result is also temporary. Ok.
  • then casting something as an lvalue reference should be the same, right? Wrong. It is an lvalue because the standard says it is. Ok.

  • temporary values, even if they have an address, are still rvalues even if the address is not made available to the programmer (this most often occurs when you return an object from a function call). okay, fine.

  • but string literals are lvalues??? & is defined on a string literal? why? why only for string literals and not all objects??????

  • accessing an element of an array from an expression for an array that is, itself an rvalue (ie, get_array()[0]) is an xvalue, not a prvalue. I guess, by definition, arrays elements are inherently addressable data available to me so sure, why not.

  • but get_object().property is also an xvalue. why. why is it an xvalue. even though objects have addresses they are not exposed to the user if they are temporary so why is the property access now considered addressable??????

I truly don't think it's possible to derive in the general case whether something is a prvalue, xvalue, or lvalue from first principles. For a lot of cases you just have to see what the standard says they are and try to make sense of it.

Maybe if I understood more about compilers there could be good reasons for these value categories but then again, if the language can only be properly understood by understanding implementation details of its compiler then it is not an effective programming language imo.

c++ 

@wallhackio in C any object literal, not just strings, can be used as an lvalue in the form

(struct my_struct){
.initialization = "values",
}

but i guess C++ is not cool enough for this?

re: c++ 

@Lady in C++ all literals are rvalues except for string literals. for Some Reason.

re: c++ 

@wallhackio probably because strings were lvalues when C++ forked from C and C added the other stuff later

re: c++ 

@wallhackio but string literals being lvalues is important in C as it lets you return them from functions

the fact that you can’t get the address of any other kind of literal is weird

re: c++ 

@Lady @wallhackio this may have something to do with how string literals are stored in the data segment in C and thus are basically just implicit global variables? i assume they work kinda the same in C++?

re: c++ 

@aescling @wallhackio string literals aren’t stored anywhere in C as the C standard does not specify storage. but they do have static storage duration

re: c++ 

@wallhackio re: the last one, object purropurrties have addresses; by accessing a purropurrty on an object you are implicitly claiming that that purropurrty is addressable. that’s just how objects work

re: c++ 

@aescling but is that address exposed to the programmer?

re: c++ 

@wallhackio @aescling well if .property is an array doesn't it have to be?

re: c++ 

@wallhackio @aescling my assumption from C, not knowing any C++, is that if you want to be able to do

char* my_string = get_object().string_val;

then you need an xvalue somewhere in there to meaningfully generate that pointer

re: c++ 

@Lady @aescling okay, so why not have only array properties, specifically, be xvalues instead of what the standard says it is, which is that any data property (that is not an enumerator member or a function) is an xvalue in this case?

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.