By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

comma operator vs. qualification conversions

P: n/a
Hi!

I am a C++ newbie and I'd like to trace the evaluation of some
expressions of my programs. I tried to use the comma operator for
that, but failed. I used a simple code substitution for expressions to
accomplish that. ... Something like ...
char * s = "foo";
.... I transformed into something like ...
extern void trace();
char * s = (trace(), "foo");
.... but my compiler (g++ 4.1.2) complained
(error: invalid conversion from 'const char*' to 'char*')!

What's wrong about my code substitution? The standard says (5.18 comma
operator):

"The type and value of the result are the type and value of the
right operand; the result is an lvalue if its right operand is."

Why does qualification conversion (4.4) not apply anymore???

-Matthias

--
Matthias Neubauer |
Universität Freiburg, Institut für Informatik | tel +49 761 203 8060
Georges-Köhler-Allee 79, 79110 Freiburg i. Br., Germany | fax +49 761 203 8052
Jun 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Matthias Neubauer <ne******@abacus.informatik.uni-freiburg.dewrote:
I am a C++ newbie and I'd like to trace the evaluation of some
expressions of my programs. I tried to use the comma operator for
that, but failed. I used a simple code substitution for expressions to
accomplish that. ... Something like ...
>char * s = "foo";

... I transformed into something like ...
>extern void trace();
char * s = (trace(), "foo");

... but my compiler (g++ 4.1.2) complained
(error: invalid conversion from 'const char*' to 'char*')!

What's wrong about my code substitution? The standard says (5.18 comma
operator):

"The type and value of the result are the type and value of the
right operand; the result is an lvalue if its right operand is."

Why does qualification conversion (4.4) not apply anymore???
I do not know why the conversion isn't happening anymore, but I get the
same thing with MSVC 8.0 (VS 2005).

However, you should be using a const char* to point to a string literal
anyway, in which case it compiles fine for me.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 25 '07 #2

P: n/a
Matthias Neubauer wrote:
Hi!

I am a C++ newbie and I'd like to trace the evaluation of some
expressions of my programs. I tried to use the comma operator for
that, but failed. I used a simple code substitution for expressions to
accomplish that. ... Something like ...
>char * s = "foo";
Remark that "foo" has normally the type array of 4 const char. But in this
situation the string literal is implicitly converted to a pointer to char,
because §4.2 allows the implicit conversion of string literals when there
exists an explicit target type. That is:
writing

char *s = "foo";
or
void foo(char*);
foo("foo");

is allowed by the standard to provide backwards compatibility. Because this
breaks const-correctness, it is deprecated. Use const char*.
... I transformed into something like ...
>extern void trace();
char * s = (trace(), "foo");

... but my compiler (g++ 4.1.2) complained
(error: invalid conversion from 'const char*' to 'char*')!
Yes. The comma expression has type array 4 of const char, which is converted
to pointer to const char.
>
What's wrong about my code substitution? The standard says (5.18 comma
operator):

"The type and value of the result are the type and value of the
right operand; the result is an lvalue if its right operand is."

Why does qualification conversion (4.4) not apply anymore???
Qualification conversion was never applied here. (4.4) allows

char *s = ...;
const char *r = s;

and not

s = r;

--
rbh
Jun 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.