Like Tree2Likes
  • 2 Post By Salem

char counts as a separate type from signed char and unsigned char??

This is a discussion on char counts as a separate type from signed char and unsigned char?? within the C++ Programming forums, part of the General Programming Boards category; I've just stumbled across something weird. Now first off, I'm aware that when you say just "char" it is implementation ...

  1. #1
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    241

    char counts as a separate type from signed char and unsigned char??

    I've just stumbled across something weird. Now first off, I'm aware that when you say just "char" it is implementation specific whether you'll get a signed or an unsigned char. But what I find surprising is that regardless of whether default char is signed or unsigned, the compiler seems to treat signed char, unsigned char and char as 3 distinct types.

    Code:
    #include <iostream>
    
    template < typename VarType >
    struct Test
    {
        inline static void test()
        {
            std::cout << "unspecified type\n";
        }
    };
    
    template <>
    struct Test< signed char >
    {
        inline static void test()
        {
            std::cout << "signed char\n";
        }
    };
    
    template <>
    struct Test< unsigned char >
    {
        inline static void test()
        {
            std::cout << "unsigned char\n";
        }
    };
    
    /*
    template <>
    struct Test< char >
    {
        inline static void test()
        {
            std::cout << "char\n";
        }
    };
    */
    
    int main()
    {
        Test< char >::test();
    }
    This example prints "unspecified type". Only if you uncomment the specialization for char and compile / run again do you get the expected (to me at least) result: "char".

    Does anyone else find that surprising?
    Last edited by antred; 07-24-2012 at 10:22 AM. Reason: fixed spelling

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by antred
    char counts as a separate type from signed char and unsigned char?
    Yes. This is stated in the standard.

    Quote Originally Posted by antred
    Does anyone else find that surprising?
    No, otherwise they should have just gone with char is signed char to be consistent with the other signed integer types.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    It's written into the standard.
    Quote Originally Posted by c++ 98
    3.9.1 Fundamental types
    1 Objects declared as characters (char) shall be large enough to store any member of the implementation’s
    basic character set. If a character from this set is stored in a character object, the integral value of that char-
    acter object is equal to the value of the single character literal form of that character. It is implementation-
    defined whether a char object can hold negative values. Characters can be explicitly declared unsigned
    or signed. Plain char, signed char, and unsigned char are three distinct types.
    stahta01 and antred like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That seems reasonable to me. If it didn't act that way then the following program would call different functions on different machines for the f(c_char) call.

    Code:
    #include <iostream>
    using namespace std;
    void f(char c) { cout << "char\n"; }
    
    void f(signed char c) { cout << "signed char\n"; }
    
    void f(unsigned char c) { cout << "unsigned char\n"; }
    
    int main()
    {
        char c_char = 'a';
        signed char c_signed_char = 'a';
        unsigned char c_unsigned_char = 'a';
    
        f(c_char);
        f(c_signed_char);
        f(c_unsigned_char);
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    241
    10 years of C++, and the language still screws me over ... scary. Thanks for your replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsigned char / signed char
    By Richardcavell in forum C Programming
    Replies: 1
    Last Post: 02-20-2011, 03:48 AM
  2. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  3. unsigned char & signed char
    By studentc in forum C Programming
    Replies: 20
    Last Post: 05-31-2004, 06:30 AM
  4. unsigned and signed char
    By P.Phant in forum C Programming
    Replies: 3
    Last Post: 07-12-2003, 05:00 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21