$4.2 states "An lvalue or rvalue of type “array of N T” or “array of
unknown bound of T” can be converted to an rvalue of type “pointer to
T”. The result is a pointer to the first element of the array."
I was not sure about what is an rvalue array and hence a little
googling threw up the following site "http://209.85.175.104/search?
q=cache:r1-djlzAyBUJ:www.open-std.org/jtc1/sc22/wg14/www/docs/n835.ps
+An+lvalue+or+rvalue+of+type+%E2%80%9Carray+of+N+T
%E2%80%9D&hl=en&ct=clnk&cd=1&gl=in"
I would like to know a little more about RValue Arrays. The standard
as far as I have understood does not clearly explain RValue arrays.
Regards,
Dabs.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
> I was not sure about what is an rvalue array and hence a little
> googling threw up the following site "http://209.85.175.104/search?q=cache:r1-djlzAyBUJ:www.open-std.org/jtc1/sc22/wg14/www/docs/n835.ps+An+lvalue+or+rvalue+of+type+%E2%80%9Carray+of+N+T%E2%80%9D&hl=en&ct=clnk&cd=1&gl=in"
>
> I would like to know a little more about RValue Arrays. The standard
> as far as I have understood does not clearly explain RValue arrays.
I think the only thing that makes array rvalues different from other
rvalues is that they never exist except as a subobject of some other
rvalue. For example:
struct X
{
int a[20];
};
Now
X()
is an rvalue of type X and
X().a
would be an rvalue array... if I'm not mistaken, that is ;-)
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
> I think the only thing that makes array rvalues different from other
> rvalues is that they never exist except as a subobject of some other
> rvalue. For example:
>
> struct X
> {
> int a[20];
> };
>
> Now
> X()
>
> is an rvalue of type X and
>
> X().a
>
> would be an rvalue array... if I'm not mistaken, that is ;-)
That was my first intention too but I'm very confused that this:
<snip>
struct X {
public:
int a[2];
};
int main() {
X().a[0]=1;
}
</snip>
actually compiles at least for g++ 4.1.2. So I can assign something
to a[0]. Although I don't have a chance to refer to it in any way
And even this:
<snip>
int main() {
int * b = X().a;
*(b+1) = 10;
}
</snip>
works. Which -- I guess -- is really fishy !!
O.
--
This is quite interesting. Normally:
X makeX();
X& x = makeX();
Generates an error because a reference to non-const can not be bound
to r-value. On the other hand:
int(&ra)[20] = makeX().a;
Does not generate an error. In other words, the compiler does not
treat a member of an r-value as an r-value.
I wonder if r-value arrays exist.
Max
--
| on Sun Jul 20 2008, rkldabs-AT-gmail.com wrote:
|
| > I was not sure about what is an rvalue array and hence a little
| > googling threw up the following site
"http://209.85.175.104/search?q=cache:r1-djlzAyBUJ:www.open-std.org/jtc1/sc22/wg14/www/docs/n835.ps+An+lvalue+or+rvalue+of+type+%E2%80%9Carray+of+N+T%E2%80%9D&hl=en&ct=clnk&cd=1&gl=in"
| >
| > I would like to know a little more about RValue Arrays. The standard
| > as far as I have understood does not clearly explain RValue arrays.
|
| I think the only thing that makes array rvalues different from other
| rvalues is that they never exist except as a subobject of some other
| rvalue. For example:
|
| struct X
| {
| int a[20];
| };
|
| Now
| X()
|
| is an rvalue of type X and
|
| X().a
|
| would be an rvalue array... if I'm not mistaken, that is ;-)
Yes, that is correct. A disturbing point is that all components of that
rvalue array are lvalues! This defect was inherited from C90 which
did not appreciate the existence of rvalue arrays.
--
Dr. Gabriel Dos Reis (g...@cs.tamu.edu), Assistant Professor
http://www.cs.tamu.edu/people/faculty/gdr
Texas A&M University -- Department of Computer Science
301, Bright Building -- College Station, TX 77843-3112
Both the initializations try to bound to a temporary and thus forbidden.
GCC complains about that pretty clearly. What compiler you use?
> I wonder if r-value arrays exist.
Yes, they do.
Well, the lvalueness is a property of expressions, not objects
themselves, so that wouldn't be correct to say that elements of an array
are lvalues or rvalues.
Then, that's hardly an example of inheriting of a problem from C89 as
there are very different models in C and C++ in this regard: in C89
rvalues are never objects while in C++ class rvalues always are. (Note
that class rvalues are the only way to refer to an rvalue array.)
C99 made things complicated by allowing array-to-pointer decays for
rvalue arrays; doing that it allows an object to part of an rvalue.
--
| Gabriel Dos Reis wrote:
| >
| > Yes, that is correct. A disturbing point is that all components of that
| > rvalue array are lvalues! This defect was inherited from C90 which
| > did not appreciate the existence of rvalue arrays.
| >
|
| Well, the lvalueness is a property of expressions, not objects
| themselves, so that wouldn't be correct to say that elements of an array
| are lvalues or rvalues.
Excuse the slopiness of `rvalue array' when I should have said
`rvalue expression designating an array'. However, In the C++
language the consequences of using an rvalue expression to designate an
object are quite deep in terms what what can be done to that object
and could not just be swept under terminology quibble.
|
| Then, that's hardly an example of inheriting of a problem from C89 as
| there are very different models in C and C++ in this regard: in C89
| rvalues are never objects while in C++ class rvalues always are. (Note
| that class rvalues are the only way to refer to an rvalue array.)
Well C90 had only the notion of `lvalue', with `rvalue being the
conceptual complement'. C++ explicitly gave a name to that
complement. But, it is not the act of giving a name to that
complement that created the problem -- which can be traced by to C90 as
subsequently corrected by WG14.
However, I do not believe WG14's fixes are acceptable for C++.
| C99 made things complicated by allowing array-to-pointer decays for
| rvalue arrays; doing that it allows an object to part of an rvalue.
However the fundamental problem goes back to C90.
--
Dr. Gabriel Dos Reis (g...@cs.tamu.edu), Assistant Professor
http://www.cs.tamu.edu/people/faculty/gdr
Texas A&M University -- Department of Computer Science
301, Bright Building -- College Station, TX 77843-3112
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
According to Comeau's online compiler, in C89 an expression accessing
a component of an rvalue array *is* itself an rvalue. Not in C++, as
you say.
Here's the code I put through the online compiler (compiles in C99 and
C++, not in C89):
typedef struct
{
int a[10];
} X;
X MakeX();
void test()
{
int* p = &MakeX().a[3];
}
James
--