Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > +1 an invalid constant expression

Reply
Thread Tools

+1 an invalid constant expression

 
 
Thad Smith
Guest
Posts: n/a
 
      01-02-2010
Francois Grieu wrote:
> the embedded C compiler that I (have to) use won't compile
> enum { f = +1 };
> giving an error message on the tune of "invalid constant
> expression", although it is fine with either of
> enum { f = 1 };
> enum { f = -1 };
>
> It similarly refuses
> int g[ +1 ];
> but accepts (including in a global context)
> int h = +1;
>
> Has there EVER been a C definition where unary + is not
> allowed in constant expressions ?
> Or a common parser with this odd characteristic ?


That isn't terribly surprising for embedded C compilers. My impression
is that sometimes the compiler is written with too many special cases,
in which general support, in this case constant expression, is lost in
less-frequency-used cases. The compiler I am using gives an error for

struct S {
int x: 2*3;
};


--
Thad
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-02-2010
Kaz Kylheku <(E-Mail Removed)> writes:
> On 2010-01-01, Eric Sosman <(E-Mail Removed)> wrote:
>> Semantically, some operators are forbidden in various types
>> of constant expressions: function calls, assignments and so on.
>> The ternary operator is not on the prohibited list for any of
>> the sub-types of constant-expression. (Or if it is, the high
>> school P.E. teacher who told me "You'll go blind" was right.)
>>
>> Where do you find a ban on the ternary operator?

>
> I seem to recall this (falsely?) from C90. Alas, I lost my hard copy.


My soft copy disagrees with your memory. The ternary operator is
permitted in constant expressions in C90.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Francois Grieu
Guest
Posts: n/a
 
      01-02-2010
Kaz Kylheku wrote:
> On 2010-01-01, Francois Grieu <(E-Mail Removed)> wrote:
>> the embedded C compiler that I (have to) use won't compile
>> enum { f = +1 };
>> giving an error message on the tune of "invalid constant
>> expression", although it is fine with either of
>> enum { f = 1 };
>> enum { f = -1 };
>>
>> It similarly refuses
>> int g[ +1 ];
>> but accepts (including in a global context)
>> int h = +1;

(..)
>
> You should report the issue to your compile vendor, but it's hardly
> a show stopper with no workaround. Why are you using unary plus at all?


// check there a some things and count them
enum { number_of_things =
#define THING(name,value) +1
#include "things.h"
#undef THING
};

with the file things.h similar to
THING(blue,12)
THING(pink,17)

My workaround is:
// count the things
enum { number_of_things = 0
#define THING(name,value) +1
#include "things.h"
#undef THING
};
// error if no thing
extern char error_no_thing[ number_of_things>0 ?1:-1 ];


Francois Grieu
 
Reply With Quote
 
Francois Grieu
Guest
Posts: n/a
 
      01-02-2010
Eric Sosman wrote :
> Original K&R C had no unary + operator at all


Ah, I did not know that. It could explain some of the
issue: start from a K&R compiler, add unary + in the
parsing of expressions, but not constant expressions.

FWIW, the compiler vendor is in business since 1983, now
uses C89 as reference, and in 2009 added some C99 traces:
enum { f = 1, }

Francois Grieu
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Case expression must be constant expression Philipp Java 26 11-25-2007 10:10 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments