are immediate strings changing from "char *" to "const char *" ?

L

Lynn McGuire

I just upgraded my web server from FreeBSD version 6.2 to version
7.2. I am now getting the warning message from gcc:
gen2wkps.cpp:400: warning: deprecated conversion from string constant to 'char*'
when I compile my server scripts. Line 400 that produced this
warning is:
cgiFormInteger ("computerid", &computerID, 0);
And the prototype is:
extern cgiFormResultType cgiFormInteger(
char *name, int *result, int defaultV);

Are immediate strings changing from "char *" to "const char *" in
the C++ standard ? Not that it matters a big deal but I am just
wondering. This will force me to use const more often and think a
little more about the effects of my programming.

My version of gcc is:
Using built-in specs.
Target: i386-undermydesk-freebsd
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 4.2.1 20070719 [FreeBSD]

Sincerely,
Lynn McGuire
 
F

Francesco S. Carta

I just upgraded my web server from FreeBSD version 6.2 to version
7.2. I am now getting the warning message from gcc:
gen2wkps.cpp:400: warning: deprecated conversion from string constant to
'char*'
when I compile my server scripts. Line 400 that produced this
warning is:
cgiFormInteger ("computerid", &computerID, 0);
And the prototype is:
extern cgiFormResultType cgiFormInteger(
char *name, int *result, int defaultV);

Are immediate strings changing from "char *" to "const char *" in
the C++ standard ? Not that it matters a big deal but I am just
wondering. This will force me to use const more often and think a
little more about the effects of my programming.

My version of gcc is:
Using built-in specs.
Target: i386-undermydesk-freebsd
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 4.2.1 20070719 [FreeBSD]

I think you just happen to be working with different compiler options
than before, as the conversion from a string literal to "char*" -
instead of their correct type in C++ which is "const char*" - was and
still is allowed by the standard for compatibility with C, though as a
deprecated feature that your compiler is warning about.

If you need to fit a previously existing C interface you can go on using
the deprecated feature. Coding new stuff in C++ should not take
advantage of this feature, of course.

See:
[conv.array] 4.2p2
[diff.lex] C.1.1 Clause 2 Subclause _lex.string
[depr.string] D.4p1
 
B

Bo Persson

Lynn said:
I just upgraded my web server from FreeBSD version 6.2 to version
7.2. I am now getting the warning message from gcc:
gen2wkps.cpp:400: warning: deprecated conversion from string
constant to 'char*' when I compile my server scripts. Line 400
that produced this warning is:
cgiFormInteger ("computerid", &computerID, 0);
And the prototype is:
extern cgiFormResultType cgiFormInteger(
char *name, int *result, int defaultV);

Are immediate strings changing from "char *" to "const char *" in
the C++ standard ? Not that it matters a big deal but I am just
wondering. This will force me to use const more often and think a
little more about the effects of my programming.

It has been that way all the time. :)

The conversion from const char* to non-const char* is allowed for
compatibility with old C code, but was deprecated already in the C++98
standard. What is new is that the compiler warns about it (and that it
seems to be forbidden in C++0x).

Adding more const seems like a good idea.


Bo Persson
 
F

Francesco S. Carta

I just upgraded my web server from FreeBSD version 6.2 to version
7.2. I am now getting the warning message from gcc:
gen2wkps.cpp:400: warning: deprecated conversion from string constant to
'char*'
when I compile my server scripts. Line 400 that produced this
warning is:
cgiFormInteger ("computerid", &computerID, 0);
And the prototype is:
extern cgiFormResultType cgiFormInteger(
char *name, int *result, int defaultV);

Are immediate strings changing from "char *" to "const char *" in
the C++ standard ? Not that it matters a big deal but I am just
wondering. This will force me to use const more often and think a
little more about the effects of my programming.

My version of gcc is:
Using built-in specs.
Target: i386-undermydesk-freebsd
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 4.2.1 20070719 [FreeBSD]

I think you just happen to be working with different compiler options
than before, as the conversion from a string literal to "char*" -
instead of their correct type in C++ which is "const char*"

Minor self nit: the correct type is "const char[n]" where "n" is the
number of effective characters in the string plus one for the appended
null character - quickly typed in layman terms, see [lex.string] 2.13.4
for the rigorous definition ;-)
 
J

James Kanze

I just upgraded my web server from FreeBSD version 6.2 to version
7.2. I am now getting the warning message from gcc:
gen2wkps.cpp:400: warning: deprecated conversion from string constant to 'char*'
when I compile my server scripts. Line 400 that produced this
warning is:
cgiFormInteger ("computerid", &computerID, 0);
And the prototype is:
extern cgiFormResultType cgiFormInteger(
char *name, int *result, int defaultV);
Are immediate strings changing from "char *" to "const char *" in
the C++ standard ?

String literals have always had the type char const[] in C++.
In the first version of the standard, there was an implicit
conversion of a string literal (but not other char const* or
char const[]) to a char*, in order to avoid breaking existing
code, but this conversion was deprecated from the start.
 
L

Lynn McGuire

Are immediate strings changing from "char *" to "const char *" in
the C++ standard ?

String literals have always had the type char const[] in C++.
In the first version of the standard, there was an implicit
conversion of a string literal (but not other char const* or
char const[]) to a char*, in order to avoid breaking existing
code, but this conversion was deprecated from the start.

I did not know that !

Thanks,
Lynn
 
L

Lynn McGuire

I think you just happen to be working with different compiler options than before, as the conversion from a string literal to "char*"
- instead of their correct type in C++ which is "const char*" - was and still is allowed by the standard for compatibility with C,
though as a deprecated feature that your compiler is warning about.

By upgrading to a newer version of FreeBSD, I got a newer version
of gcc. Which appears to have a new warning or the warning is now
turned on at a lower level.

Thanks,
Lynn
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top