www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Literal Strings as const char *?

↑ ↓ ← 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
↑ ↓ "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

 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.

 I guess - that this would break so much existing code as to be useless.

 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
↑ ↓ → 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

 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.

 I guess - that this would break so much existing code as to be useless.

 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