Topic: Q:gcc version 2.95 19990728 (release)


Author: "Michael N. Filippov" <michael@idisys.iae.nsk.su>
Date: 1999/12/24
Raw View

It doesn't look logical, that it differs from that long construction
with if{}else{} (additional type conversion) but standard is standard :)

Thanks a lot

Michael


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Michael N. Filippov" <michael@idisys.iae.nsk.su>
Date: 1999/12/22
Raw View
Hello !
Can somebody explain this ? (difference between 2 variants)

idisys:/usr1/users/michael/tmp$ cat const.cpp
#include <stdio.h>

void    func(char* s)
{
  printf("%s'\n", s);
}

int  main(void)
{
  char  *p = "test string";     // NULL;

#ifdef  LONG_ONE
  if ( p == NULL )
    func("");
  else
    func(p);
#else //LONG_ONE
  func( ( p == NULL ) ? "" : p );
#endif//LONG_ONE

  return 0;
}

idisys:/usr1/users/michael/tmp$ g++ const.cpp
const.cpp: In function  nt main()':
const.cpp:18: passing `const char *' as argument 1 of `func(char *)' discards qualifiers
idisys:/usr1/users/michael/tmp$ g++ -DLONG_ONE const.cpp
idisys:/usr1/users/michael/tmp$ g++ -v
Reading specs from /usr/lib/gcc-lib/i586-ksi-linux/2.95/specs
gcc version 2.95 19990728 (release)
idisys:/usr1/users/michael/tmp$


Thanks in advance,
Michael
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1999/12/23
Raw View
On Dec 22, 1999, "Michael N. Filippov" <michael@idisys.iae.nsk.su> wrote:

> Can somebody explain this?

>   func( ( p == NULL ) ? "" : p );

> const.cpp:18: passing `const char *' as argument 1 of `func(char *)' discards qualifiers

The C++ Standard defines a deprecated conversion from string literals
(of type `char const[N]') to non-const `char*'.  However, there's no
reason for such a conversion within the ?: expression.  Merging the
types of `""' and `p' results `const char*', thus the error.

--
Alexandre Oliva http://www.ic.unicamp.br/~oliva IC-Unicamp, Bra[sz]il
oliva@{lsd.ic.unicamp.br,guarana.{org,com}} aoliva@{acm,computer}.org
oliva@{gnu.org,kaffe.org,{egcs,sourceware}.cygnus.com,samba.org}
** I may forward mail about projects to mailing lists; please use them
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: deepblack@geocities.com (Luis Coelho)
Date: 1999/12/23
Raw View
On 22 Dec 99 12:27:23 GMT, "Michael N. Filippov"
<michael@idisys.iae.nsk.su> uttered the following words:

>Hello !
>Can somebody explain this ? (difference between 2 variants)
<snip>
>  func( ( p == NULL ) ? "" : p );

The type of a cond ? alt1 : alt2 expression is the lowest common
denominator of alt1 and alt2 (I'm sure there is a more precise
description, but this is IMHO a good way to think about it). In
this case "" is _const_ char* and p char*, so the the type of
cond ? "" : p is const char*!

Since func takes a char*, you have a type mismatch.

HTH,
Luis Coelho.
C++ Programming Language, 3rd Ed. by B. Stroustrup. My exercise answers at:
http://www.geocities.com/SiliconValley/Way/3972/index.html
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]