static_cast problem

Ulrich Drepper drepper@cygnus.com
Fri Dec 31 20:54:00 GMT 1999


I sumpled accross this problem in the current mainline gcc while
compiling Mozilla:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class foo
{
  int a;

public:
  operator const char *() { return (const char *) &a; }
  foo(int b = 10) { a = b; }
};

int
main()
{
  foo a;
#ifndef BUG
  const char *t(a);
  return (const char *) a == 0;
#else
  return static_cast<const char *>(a) == 0;
#endif
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you compile it without BUG defined it works just fine.  But with
BUG defined gcc complains

static_cast from `foo' to `const char'

(not a very informative message).  Anyway, the C++ standard says that
the static_cast<T>(e) should succeed if a definition

	T t(e)

is possible.  But this is just what is done on the !BUG case.

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------



More information about the Gcc-bugs mailing list