How to read "The lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversionsare

L

Lighter

How to read "The lvalue-to-rvalue, array-to-pointer, and function-to-
pointer standard conversionsare not applied to the left expressions"?

In 5.18 Comma operator of the C++ standard, there is a rule:

"The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
standard conversionsare not applied to the left expressions"

I could not understand what the rule means. I also searched the web
and the groups and got nothing.

Who can give me some tips? Many thanks in advance.
 
J

James Kanze

How to read "The lvalue-to-rvalue, array-to-pointer, and function-to-
pointer standard conversionsare not applied to the left expressions"?
In 5.18 Comma operator of the C++ standard, there is a rule:
"The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
standard conversions are not applied to the left expressions"
I could not understand what the rule means. I also searched the web
and the groups and got nothing.

Well, it means that the lvalue-to-rule, array-to-pointer and
function-to-pointer conversions are not applied to the left
operand of a comma operator. That seems clear enough, per se;
what part don't you understand?

In general, I can't really see how a conforming program could
tell whether they were applied or not. For example, if I write:

int array[ 10 ] ;

array, f() ;

how can I possibly know whether the type of the right operand of
the comma is int[10] or int*? And how can it matter?

I suppose that it is really only significant with regards to
whether the type must be complete, or whether there must be a
definition.

class C ;
C* pC ;

*pC, f()

is legal, but it wouldn't be if an lvalue-to-rvalue conversion
took place (since the standard requires a complete type any time
there is an lvalue-to-rvalue conversion). Similarly, given:

extern int g() ;
extern int a[] ;

g, f() ;
a, f() ;

I think (but I'm far from sure) the fact that the function to
pointer conversion doesn't take place on g means that g is not
used in this expression, and thus that no definition is
required. And the same thing holds for a, because the
array-to-pointer conversion doesn't take place.

At any rate, that's the only real relevance I can see.
 
L

Lighter

Thank you very much, James.

Please see an example:

int a[2];
int* p;
int* p2 = (p = a, p++); // Note here, p = a is a array-to-pointer
conversion.

the code above can be compiled without any warning with VS 2005. Why?

What is the reason of the standard's making such a rule? That is what
I really want to see. Could give me an explaination?
 
R

Ron Natalie

Lighter said:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to-
pointer standard conversionsare not applied to the left expressions"?

In 5.18 Comma operator of the C++ standard, there is a rule:

"The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
standard conversionsare not applied to the left expressions"

I could not understand what the rule means. I also searched the web
and the groups and got nothing.

Who can give me some tips? Many thanks in advance.
Generally, it's meaningless. Since the value is just discarded, the
conversions don't happen. I'm not sure how you'd tell if they were
to happen anyhow.

int scalar = 0;
int array[10] = { 0 };
void func();

int i = scalar , 0;
i = array , 0;
i = func , 0;

Means the value scalar isn't converted to an rvalue,
and array isn't converted to int*,
and func isn't converted to void (*)().
 
V

Victor Bazarov

Ron said:
Lighter said:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to-
pointer standard conversionsare not applied to the left
expressions"?

In 5.18 Comma operator of the C++ standard, there is a rule:

"The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
standard conversionsare not applied to the left expressions"

I could not understand what the rule means. I also searched the web
and the groups and got nothing.

Who can give me some tips? Many thanks in advance.
Generally, it's meaningless. Since the value is just discarded, the
conversions don't happen. I'm not sure how you'd tell if they were
to happen anyhow.

int scalar = 0;
int array[10] = { 0 };
void func();

int i = scalar , 0;
i = array , 0;
i = func , 0;

I don't believe this should compile. You need parentheses around the
comma expressions.
Means the value scalar isn't converted to an rvalue,
and array isn't converted to int*,
and func isn't converted to void (*)().

Take a look at this program:
--------------------------------------
int main() {
int a;
a, 42;
}
--------------------------------------
IF lvalue-to-rvalue conversion were happening for 'a', then the program
would have undefined behaviour because 'a' is uninitialised. I don't
think there is any other real need/implication for the rule.

V
 
J

James Kanze

Thank you very much, James.
Please see an example:
int a[2];
int* p;
int* p2 = (p = a, p++); // Note here, p = a is a array-to-pointer
conversion.

OK. I understand where your problem is.

"p = a" is not an array-to-pointer conversion, because the type
of "p = a" is int*; the type of an assignment expression is
always the type of the lvalue being assigned.

There is an array-to-pointer conversion *in* the expression, but
the expression itself isn't subject to the conversion: if the
results of "p = a" were an array, no conversion would take
place. The expression "a" is not the expression to the right of
the comma operator; the expression "p = a" is. The expression
"a" is the expression to the left of an assignment operator,
where the rvalue to lvalue conversion takes place (which
automatically means that the array-to-pointer conversion is
active).
the code above can be compiled without any warning with VS 2005. Why?

Because it's legal.
What is the reason of the standard's making such a rule? That is what
I really want to see. Could give me an explaination?

See above. The text concerns only the operands of the comma
operator. The sub-expressions of that expression are not
concerned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top