CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Sign of int

  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Sign of int

    Whenever you write a data type as "int", is it a guaranteed alias for "signed int", or is that defined by the compilation options?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Sign of int

    I've seen compilers that offer "default char to unsigned" as an option, but I havent seen "int to unsigned".
    your humble savant

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Sign of int

    Quote Originally Posted by TheRogue View Post
    I've seen compilers that offer "default char to unsigned" as an option, but I havent seen "int to unsigned".
    Yeah.

    However, "char" is it's own data type which is different from "unsigned char" and "signed char", so that option defines how "char" should behave. Even if "char" is unsigned, it is not the same data type as "unsigned char".

    int, on the other hand, as far as I know, is jus short-hand for signed int. I'm 90% sure this is guaranteed by the standard, but I want to make sure.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Sign of int

    3.9.1 lists "int" as one of the five standard signed integer types.
    your humble savant

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Sign of int

    Quote Originally Posted by monarch_dodra View Post
    I'm 90% sure this is guaranteed by the standard, but I want to make sure.
    well, you can easily test this

    Code:
    template <class T> struct Test{};
    
    template <> struct Test<char>{};
    template <> struct Test<signed char>{};
    template <> struct Test<unsigned char>{};
    
    template <> struct Test<int>{};
    template <> struct Test<signed int>{};
    template <> struct Test<unsigned int>{};
    Comeau gives

    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing. All rights reserved.
    MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 8: error: class "Test<int>" has already been defined
    template <> struct Test<signed int>{};
    ^

    1 error detected in the compilation of "ComeauTest.c".

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Talking Re: Sign of int

    Quote Originally Posted by superbonzo View Post
    well, you can easily test this
    Testing the specific implementation of a specific compiler does not test what the standard says.
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Sign of int

    Quote Originally Posted by TheRogue View Post
    3.9.1 lists "int" as one of the five standard signed integer types.
    Indeed, that seems to answer my question. Thanks.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Sign of int

    Quote Originally Posted by monarch_dodra View Post
    However, "char" is it's own data type which is different from "unsigned char" and "signed char"
    I wonder when that is going to be fully supported? It would sure be wonderful to get numbers printed even when forgetting to cast when using << on unsigned chars...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Sign of int

    Quote Originally Posted by S_M_A View Post
    I wonder when that is going to be fully supported? It would sure be wonderful to get numbers printed even when forgetting to cast when using << on unsigned chars...
    It's supported on both my GCC and VS2008. I think the "problem" is that the operator<< on ostream has the same behaviour for "char", "unsigned char" and "signed char", which is to print the character, and not the value.

    The easy fix is to just call the unary operator+ first:

    Code:
    unsigned char myChar = ...;
    
    ...
    
    out << +myChar;
    operator+ will trigger type promotion, turning unsigned chars into unsigned ints, signed chars into signed ints, and leave everything else un-changed.*

    *Except shorts, if you want to get technical.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Sign of int

    Genius, I love it.
    My hobby projects:
    www.rclsoftware.org.uk

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Sign of int

    Absolutely a beautiful little trick that saves a lot of typing!

    I still think the stream operators should print characters when fed with a char and numbers for an unsigned/signed char. After all the types are separated by the standard. Does anybody know why the compilers (all?) do not support it?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured