floating-point literal cannot appear in a constant-expression

T

TimC

gcc 4.x -pedantic doesn't allow me to do this:

static const long int DOME_WANDER = int(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

and gives:
In file included from TcsEncoderTask.C:232:
AbsIncPositionTracker.h:189: error: floating-point literal cannot appear in a constant-expression

First off, is that truly illegal code (TcsAbsDomeToArcsec and
TcsIncDomeToArcsec are just #defines of floats) according to C99?

And secondly, if this is truly illegal and not just a mundane bug in
gcc, tell me, what would be the point of illegalising that, and what
do I do to get around it? Surely a cast (it's been a while since I've
done C, so I tried several different casts, none of which worked) is
sufficient to convince both a compiler and a language lawyer that yes,
I know it's a float, but an int is perfectly acceptable for my
purposes? Initialising DOME_WANDER to be a pure int not calculated
from those above floats would be fragile and a serious pain.
 
N

Nate Eldredge

TimC said:
gcc 4.x -pedantic doesn't allow me to do this:

static const long int DOME_WANDER = int(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

Probably you meant

static const long int DOME_WANDER = (int)(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

or more likely

static const long int DOME_WANDER = (long int)(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

Those compile fine for me.
and gives:
In file included from TcsEncoderTask.C:232:
AbsIncPositionTracker.h:189: error: floating-point literal cannot appear in a constant-expression

Strange error message, but what you wrote isn't legal.
 
N

Nate Eldredge

TimC said:
gcc 4.x -pedantic doesn't allow me to do this:

static const long int DOME_WANDER = int(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

Probably you meant

static const long int DOME_WANDER = (int)(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

or more likely

static const long int DOME_WANDER = (long int)(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

Those compile fine for me.
and gives:
In file included from TcsEncoderTask.C:232:
AbsIncPositionTracker.h:189: error: floating-point literal cannot appear in a constant-expression

Strange error message, but what you wrote isn't legal.
 
M

Martin Ambuhl

TimC said:
gcc 4.x -pedantic doesn't allow me to do this:

static const long int DOME_WANDER = int(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

There is no such function as int() in C.
Try the following, which should give you no diagnostics at all:

#include <stdio.h>

/* defining the terms needed for compile-time constants */
#define TcsAbsDomeToArcsec 47.
#define TcsIncDomeToArcsec 3.2

/* A file-scope form of your declaration with the non-function int()
removed */

static const long int DOME_WANDER1 =
TcsAbsDomeToArcsec * 1.25 / TcsIncDomeToArcsec;

int main(void)
{
/* A block-scope form of your declaration with the non-function int()
removed */

static const long int DOME_WANDER2 =
TcsAbsDomeToArcsec * 1.25 / TcsIncDomeToArcsec;
return 0;
}

and gives:
In file included from TcsEncoderTask.C:232:
AbsIncPositionTracker.h:189: error: floating-point literal cannot appear in a constant-expression

If it didn't complain about int(), then you are not using gcc as a C
compiler.
First off, is that truly illegal code (TcsAbsDomeToArcsec and
TcsIncDomeToArcsec are just #defines of floats) according to C99?

See the code I posted above. There is nothing illegal about
(TcsAbsDomeToArcsec * 1.25 / TcsIncDomeToArcsec) in an initializer. But
we've already established that you are not compiling your code as C. In
some other language it might well be illegal.

Surely a cast (it's been a while since I've
done C, so I tried several different casts, none of which worked) is
sufficient to convince both a compiler and a language lawyer that yes,
I know it's a float, but an int is perfectly acceptable for my
purposes?

It is not only sufficient, but completely unneccesary. There are no
casts at all in the code I posted above and it does just what you want.
Further, you used no casts at all, anyway. To cast the value of a
floating-point value (call it 'x') to int, a cast in C looks like
(int) x
not
int(x)
which is an error.
 
M

Mark L Pappin

Nate Eldredge said:
Strange error message, but what you wrote isn't legal.

*.c ==> gcc is a C compiler
*.C ==> gcc is a C++ compiler

C and C++ have different ideas about constant-expressions, and about
the necessary syntax for casts.

mlp
 
T

TimC

If it didn't complain about int(), then you are not using gcc as a C
compiler.

You're quite right. I had assumed it was C even though it was (by
default, although I tried gcc as well) being compiled with g++ (I
myself have compiled code with g++ before just to get the more strict
warnings), but upon a closer look I realised it really was C++ code,
and I guess gcc must be passing it off to g++.
See the code I posted above. There is nothing illegal about
(TcsAbsDomeToArcsec * 1.25 / TcsIncDomeToArcsec) in an initializer. But
we've already established that you are not compiling your code as C. In
some other language it might well be illegal.

It compiles as long as it is not declared within a class.

I shall now head over to c.l.c++
It is not only sufficient, but completely unneccesary. There are no
casts at all in the code I posted above and it does just what you want.
Further, you used no casts at all, anyway. To cast the value of a
floating-point value (call it 'x') to int, a cast in C looks like
(int) x
not
int(x)
which is an error.

Yep. Strangely, your minimal code does give warnings in gcc 4.1.2 but
not in 4.3.3:
/tmp/foo.C:11: warning: converting to $B!F(Blong int$B!G(B from $B!F(Bdouble$B!G(B
/tmp/foo.C: In function $B!F(Bint main()$B!G(B:
/tmp/foo.C:19: warning: converting to $B!F(Blong int$B!G(B from $B!F(Bdouble$B!G(B
 
C

CBFalconer

TimC said:
gcc 4.x -pedantic doesn't allow me to do this:

static const long int DOME_WANDER = int(TcsAbsDomeToArcsec*1.25/TcsIncDomeToArcsec);

and gives:
In file included from TcsEncoderTask.C:232:
AbsIncPositionTracker.h:189: error: floating-point literal cannot appear in a constant-expression

First off, is that truly illegal code (TcsAbsDomeToArcsec and
TcsIncDomeToArcsec are just #defines of floats) according to C99?

Yes. DOME_WANDER is not a constant expression. It is a read-only
object, which could only be set in its declaration statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top