void foo( char* ); int main() { foo( 0 ? "a" : "b" ); // } The type of expression (0 ? "a" : "b") is const char[2] but it cannot be transformed to char*, however it works. Why?
earth:~>g++ t.cc -pedantic -Wwrite-strings t.cc: In function ‘int main()’: t.cc:5: warning: deprecated conversion from string constant to ‘char*’'
(In reply to comment #1) > earth:~>g++ t.cc -pedantic -Wwrite-strings > t.cc: In function ‘int main()’: > t.cc:5: warning: deprecated conversion from string constant to ‘char*’' > Conversion from string constant to ‘char*’' it's legacy from C, but GCC should not perceive it as const char*, because the type of expression is const char[]! I think GCC have defined it as a compile-time constant, but it's infeasible solution. In other situation it behaves correctly: void foo( char* ); int main() { bool flag = false; foo( flag ? "a" : "b" ); // error ( cannot convert from const char* to char* ) }
I have not looked into the standard yet but if GCC's warning message is correct this is valid but deprecated code which allows for a compiler to accept it or not.
(In reply to comment #3) > I have not looked into the standard yet but if GCC's warning message is correct > this is valid but deprecated code which allows for a compiler to accept it or > not. > deprecated this converiont: void foo( char* ) { } int main() { foo( "lalala" ); } i.e. convresion from strings literal to char*, but the result of expression (0 ? "a" : "b") IS NOT STRING LITERAL!!! IT'S CONST CHAR[2]!!! ( See 5.16 )
Confirmed. These two lines should behave the same, but don't: -------------- extern bool flag; int main() { char* p; p = (true ? "a" : "lalala" ); p = (flag ? "a" : "lalala" ); } -------------- g/x> /home/bangerth/bin/gcc-4.2*/bin/c++ -c x.cc -Wwrite-strings x.cc: In function ‘int main()’: x.cc:5: warning: deprecated conversion from string constant to ‘char*’' x.cc:6: error: invalid conversion from ‘const char*’ to ‘char*’ W.
Mine.