Bug 25992 - conditional expression and strings literal
Summary: conditional expression and strings literal
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.5
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2006-01-27 10:39 UTC by Anton Kirillov
Modified: 2020-09-18 13:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-03-08 05:02:36


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anton Kirillov 2006-01-27 10:39:50 UTC
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?
Comment 1 Andrew Pinski 2006-01-27 12:39:50 UTC
earth:~>g++ t.cc -pedantic -Wwrite-strings
t.cc: In function ‘int main()’:
t.cc:5: warning: deprecated conversion from string constant to ‘char*’'

Comment 2 Anton Kirillov 2006-01-27 13:12:32 UTC
(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* )
}
Comment 3 Andrew Pinski 2006-01-27 13:59:29 UTC
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.
Comment 4 Anton Kirillov 2006-01-27 14:20:44 UTC
(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 )
Comment 5 Wolfgang Bangerth 2006-03-08 05:02:36 UTC
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.
Comment 6 Paolo Carlini 2014-07-15 12:43:07 UTC
Mine.