www.digitalmars.com         C & C++   DMDScript  

c++ - Literal Strings as const char *?

reply John Jameson <John_member pathlink.com> writes:
I am a little confused. Am using DMC 8.44 (beta) which in which literal strings
are taken as constant. Except when they aren't...

Given the following

void foo (char *);

main ()

{ char *p = "hello";
int f= 1;
foo("hello");
foo(p);
foo (f ? "a" : "b");
foo (f ? p :  "b");
}   


foo (f ? "a" : "b");
^
test.cpp(9) : Error: need explicit cast for function parameter 1 to get
from: char const *
to  : char *
foo (f ? p :  "b");
^
test.cpp(10) : Error: need explicit cast for function parameter 1 to get
from: char const *
to  : char *
--- errorlevel 1

It seems to me that the first of the function calls should be an error. Except -
I guess - that this would break so much existing code as to be useless. But then
if we are to allow that, then why not the tertiaries? 

Cheers,
John.
Jul 25 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"John Jameson" <John_member pathlink.com> wrote in message
news:dc2joe$1l01$1 digitaldaemon.com...
 I am a little confused. Am using DMC 8.44 (beta) which in which literal
strings
 are taken as constant. Except when they aren't...

 Given the following

 void foo (char *);

 main ()

 { char *p = "hello";
 int f= 1;
 foo("hello");
 foo(p);
 foo (f ? "a" : "b");
 foo (f ? p :  "b");
 }


 foo (f ? "a" : "b");
 ^
 test.cpp(9) : Error: need explicit cast for function parameter 1 to get
 from: char const *
 to  : char *
 foo (f ? p :  "b");
 ^
 test.cpp(10) : Error: need explicit cast for function parameter 1 to get
 from: char const *
 to  : char *
 --- errorlevel 1

 It seems to me that the first of the function calls should be an error.
Except -
 I guess - that this would break so much existing code as to be useless.
But then
 if we are to allow that, then why not the tertiaries?
I thought about that case when implementing it, but could find no language in the C++ Standard which supported the implicit conversion for the ?: expressions - it's only for "string literals". Perhaps my interpretation is wrong, but I thought I'd try it this way.
Jul 29 2005
parent John Jameson <John_member pathlink.com> writes:
Well, I guess I should be grateful that the mighty standards committee at least
decided to allow an exception for simple char *arguments :-) 

Anyway, am pleased to report that we have now ported all the core sections of
Proteus from SC 7.5 to DM 8.44 beta with no sign of new bugs or problems so far.

thanks once again for putting the -Ai switch in.

Cheers,
john


In article <dcet0r$2srr$1 digitaldaemon.com>, Walter says...
"John Jameson" <John_member pathlink.com> wrote in message
news:dc2joe$1l01$1 digitaldaemon.com...
 I am a little confused. Am using DMC 8.44 (beta) which in which literal
strings
 are taken as constant. Except when they aren't...

 Given the following

 void foo (char *);

 main ()

 { char *p = "hello";
 int f= 1;
 foo("hello");
 foo(p);
 foo (f ? "a" : "b");
 foo (f ? p :  "b");
 }


 foo (f ? "a" : "b");
 ^
 test.cpp(9) : Error: need explicit cast for function parameter 1 to get
 from: char const *
 to  : char *
 foo (f ? p :  "b");
 ^
 test.cpp(10) : Error: need explicit cast for function parameter 1 to get
 from: char const *
 to  : char *
 --- errorlevel 1

 It seems to me that the first of the function calls should be an error.
Except -
 I guess - that this would break so much existing code as to be useless.
But then
 if we are to allow that, then why not the tertiaries?
I thought about that case when implementing it, but could find no language in the C++ Standard which supported the implicit conversion for the ?: expressions - it's only for "string literals". Perhaps my interpretation is wrong, but I thought I'd try it this way.
Aug 02 2005