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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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