#include "stdio.h" void test(char *){} int main(int argc, char* argv[]) { test("test1"); int i = 1; test(i?"test2":"test3"); return 0; } returns the error: test.cpp: In function `int main (int, char **)': test.cpp:7: cannot convert `const char *' to `char *' for argument `1' to `test (char *)' kgcc and other compilers work fine. even the first statement works fine.
That code is buggy, you should pass string constants to const char * function arguments, not char *, because unless -fwriteable-strings, test won't be able to write into that string. So, either change test to test(const char *), or, if you make yourself sure that test never modifies the string and don't want to leave that checking on g++, you could call test((char *)"test"); g++ 2.96 is much more picky about C++ standard compliance, so it will reject more invalid inputs than it used to in the past.
Created attachment 3945 [details] sample source
If this is true, why does the first statement work? Either way its a bug, either the first test, or the second test.
I don't have C++ standard with me here at home, so I cannot tell exactly why it works that way, but already g++ 2.95 refuses your testcase (and only in the second test invocation, not first).
IMO, it is a compiler bug because there is no difference between the string literals involved in both calls of test(). Implicit conversion of a const char* to non-const char* is deprecated. It still works in order to not break old C++ code from times when string literals where (non-const) pointers to char. But code like this test case ought not be written. Its result at runtime is undefined. Watch this: #include <stdio.h> void test(char* s) { s[0] = 'f'; // uh oh! } int main(int argc, char* argv[]) { test("test1"); int i = 1; //test(i?"test2":"test3"); return 0; } Segmentation fault (core dumped)
It is actually not a bug, just a permissive g++ in the first case. Really, if g++ would not be permissive, you would not be able to compile most of the broken C++ code out there (because passing string constants to char * arguments is very common). I believe it will be changed in GCC4 or something, after some period of just warning about it.
Nevertheless, the compiler ought to allow the second test case in the first code example. It ought to perform implicit conversion of const char* to non-const char* in this line: test(i?"test2":"test3");
$ g++ -Wwrite-strings test.cpp -c test.cpp: In function `int main (int, char **)': test.cpp:10: warning: deprecated conversion from string constant to `char *' test.cpp:12: cannot convert `const char *' to `char *' for argument `1' to `test (char *)' Either warning or error. But both does not make sense.