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

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

    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    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.

  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
    257
    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, 04: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, 01:30 PM