f



Are negative integer literals allowed in std C++?

>From the definition of an integer literal in the 1998 ISO/IEC 14882
standard (in 2.13.1) (or the preprocessing number definition in 2.9), I
don't see a way for negative integer literals to be defined.  (I'm
thinking it must be covered elsewhere, but I haven't come across it,
yet.)  Although the text in 2.13.1 suggests that it's possible, based
on the suffix (or, more specifically, the absence of the 'U' suffix),
and although I've done it in practice (so compilers at least allow it),
there doesn't seem to be allowance for a leading sign (+/-) character.
I understand this for hexadecimal and octal integer representations,
but I'd expect to be able to use a signed decimal representation for an
integer literal (at least, for one that is not defined as being
unsigned, according to the suffix).  How does one define a negative
integer literal using a decimal representation?  Can someone point me
to the relevant section in the std that defines this?  I find it hard
to believe that something so fundamental would not be explicitly
defined and/or left as "implementation dependent".

The way I see it, I can't write either of the following in a portable
manner defined by the standard.  I could almost understand if there
were additional restrictions on the CPP token processing in the second
example, so that the constant expression in a conditional directive
would not be defined when negative literals are involved.  But the
first example seems way too common to be undefined in the standard.
What am I missing?

  int n( -1 ) ;

   #if ( -1 == x )
      // code here
   #endif

Thanks,
     Mike


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
12/12/2006 1:53:01 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8510) is leader. Post Follow

2 Replies
358 Views

Similar Articles

[PageSpeed] 2

michael.young@paetec.com wrote:
> >From the definition of an integer literal in the 1998 ISO/IEC 14882
> standard (in 2.13.1) (or the preprocessing number definition in 2.9), I
> don't see a way for negative integer literals to be defined.  (I'm
> thinking it must be covered elsewhere, but I haven't come across it,
> yet.)  Although the text in 2.13.1 suggests that it's possible, based
> on the suffix (or, more specifically, the absence of the 'U' suffix),
> and although I've done it in practice (so compilers at least allow it),
> there doesn't seem to be allowance for a leading sign (+/-) character.
> I understand this for hexadecimal and octal integer representations,
> but I'd expect to be able to use a signed decimal representation for an
> integer literal (at least, for one that is not defined as being
> unsigned, according to the suffix).  How does one define a negative
> integer literal using a decimal representation?  Can someone point me
> to the relevant section in the std that defines this?  I find it hard
> to believe that something so fundamental would not be explicitly
> defined and/or left as "implementation dependent".
>
> The way I see it, I can't write either of the following in a portable
> manner defined by the standard.  I could almost understand if there
> were additional restrictions on the CPP token processing in the second
> example, so that the constant expression in a conditional directive
> would not be defined when negative literals are involved.  But the
> first example seems way too common to be undefined in the standard.
> What am I missing?
>
>   int n( -1 ) ;
>
>    #if ( -1 == x )
>       // code here
>    #endif
>
Hmmm,,, we just covered this yesterday on comp.lang.c++.

It seems the answer is no, literals are positive.  However you can
combine the unary negative operator with a literal to accomplish what
seems for all intensive purpose to be a negative literal.  The only
difference, that was pointed out by Alf, is with the largest possible
negative integer.

So this statement is technically undefined (on 32 bit machine) AFAIK
and produces a warning on g++:

std::cout << -2147483647 << std::endl;

----
Ivan
http://www.0x4849.net


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Ivan
12/12/2006 5:30:22 AM
On 11 Dec 2006 20:53:01 -0500, michael.young@paetec.com wrote in
comp.lang.c++.moderated:

> >From the definition of an integer literal in the 1998 ISO/IEC 14882
> standard (in 2.13.1) (or the preprocessing number definition in 2.9), I
> don't see a way for negative integer literals to be defined.  (I'm
> thinking it must be covered elsewhere, but I haven't come across it,
> yet.)  Although the text in 2.13.1 suggests that it's possible, based
> on the suffix (or, more specifically, the absence of the 'U' suffix),
> and although I've done it in practice (so compilers at least allow it),
> there doesn't seem to be allowance for a leading sign (+/-) character.
> I understand this for hexadecimal and octal integer representations,
> but I'd expect to be able to use a signed decimal representation for an
> integer literal (at least, for one that is not defined as being
> unsigned, according to the suffix).  How does one define a negative
> integer literal using a decimal representation?  Can someone point me
> to the relevant section in the std that defines this?  I find it hard
> to believe that something so fundamental would not be explicitly
> defined and/or left as "implementation dependent".

It is quite simple, there is no such thing as an integer or floating
point literal in C++, just as there is no such thing as an integer or
floating point constant expression in C, which is where C++ inherited
the implementation.

The expression -1 consists of two distinct elements, the integer
literal 1 and the unary negation operator.

> The way I see it, I can't write either of the following in a portable
> manner defined by the standard.  I could almost understand if there
> were additional restrictions on the CPP token processing in the second
> example, so that the constant expression in a conditional directive
> would not be defined when negative literals are involved.  But the
> first example seems way too common to be undefined in the standard.
> What am I missing?
> 
>   int n( -1 ) ;
> 
>    #if ( -1 == x )
>       // code here
>    #endif
> 
> Thanks,
>      Mike

All of the following work the same way:

unsigned int ui = ~1U;  // sets ui to UINT_MAX - 1

bool b = !1;  // sets b to false

There are no negative literals in C++, there are no bit-wise
complemented literals in C++, and there are no logically negated
literals in C++.

But there are constant expressions that can be formed by the
combination of a literal and a unary operator appropriate to the type
of the literal.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Jack
12/12/2006 5:32:17 AM
Reply: