473,509 Members | 3,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comma operator vs. qualification conversions

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
2 1604
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8018
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
5
2540
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
7
2075
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
11
2361
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
21
3004
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
2
9770
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
6
2329
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
15
2610
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
1
1841
by: Neelesh Bodas | last post by:
I think I understand this, but want to confirm. Is the following statement correct? ("yes" acc. to my understanding) <quote> A qualification conversion talks about pointer types, not about non-...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.