This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: g++4.0 diagnostic on casting a static const member initializer to a double
- From: Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- To: Mark Mitchell <mark at codesourcery dot com>
- Cc: Fariborz Jahanian <fjahanian at apple dot com>, gcc at gcc dot gnu dot org
- Date: Mon, 31 Jan 2005 18:28:55 +0000
- Subject: Re: g++4.0 diagnostic on casting a static const member initializer to a double
- References: <539EBBFC-73AE-11D9-8C44-000393B9ED88@apple.com> <41FE75E3.8080602@codesourcery.com>
On Mon, Jan 31, 2005 at 10:16:03AM -0800, Mark Mitchell wrote:
> Fariborz Jahanian wrote:
> >g++ 4.0 in FSF issues a diagnostic on the following test case:
> >
> >template <class T> struct S {
> > static const double scd = (double)24;
> >};
> >
> >test1.C:2: error: a cast to a type other than an integral or enumeration
> >type cannot appear in a constant-expression
> >
> >Rewriting the above to:
> >
> >template <class T> struct S {
> > static const double scd;
> >};
> >
> >template <class T>
> >const double S<T>::scd = (double)24;
> >
> >Compiles. Event though the diagnostic is clear in what it is saying, but
> >it seems to be a g++ bug
> >one way or the other.
>
> No, the input program is invalid. From [class.static.data]:
>
> "If a static data member is of const integral or const enumeration
> type, its declaration in the class definition can specify a
> constant-initializer which shall be an integral constant
> expression(_expr.const_)."
>
> The diagnostic could be better, but it is indeed accurate; if there's an
> initializer it must be integral constant-expression, and a cast to
> "double" cannot appear in such an expression.
There seems to be a (possibly undocumented) GCC extension involved here.
[class.static.data] should also prevent this compiling, but unless you
use -pedantic both 3.4 and 4.0 allow it:
struct S {
static const double scd = 24.0;
};
I didn't realise this worked until seeing the diagnostic quoted above.
I thought that used to fail with 3.4, was I wrong?
jon
--
"A well-written program is its own heaven
A poorly written program is its own hell"
- The Tao of Programming