Thread: Comparing characters

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    Comparing characters

    Do I need to do something special with character constants if my code compares a template type with them?
    Code:
    template <typename T>
    bool isallzeros(const basic_string<T>& str)
    {
        for ( int i = 0; i < str.length(); ++i )
        {
            if ( str[i] != '0' )
                return false;
        }
    
        return true;
    }
    If T is wchar_t, will comparing str[i] to '0' still work or do I have to do something special with '0'?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will work fine.
    You only have to be careful with strings, but if you use basic_string, there's no need for worry at all.
    (Though, case in point, if it's wchar and you're comparing strings, you have to add L, so you can use the macro _T or TEXT to automatically append L if it's unicode.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    It will work fine.
    Why? A wide character constant starts with L. If str[i] is a wchar_t then shouldn't I be using L'0'? Where do I find _T or TEXT?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope, a wide string begins with L, not a wide character.
    _T or TEXT is defined in Visual Studio, don't know if they're defined in other compilers. Otherwise you could try to define them yourself depending on if you're using unicode or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    http://msdn2.microsoft.com/en-us/library/6aw8xdf2.aspx

    It looks like wide strings and wide characters both start with L.

    Otherwise you could try to define them yourself depending on if you're using unicode or not.
    That's my problem. T is a template type and I have no way of telling if it's char or wchar_t or some other custom type but I still need to compare it with '0'.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	char c = 'c';
    	wchar_t w = 'w';
    Try it yourself. It will compile.

    What I was referring to is if you're using unicode in your app or not.
    And if you can't easily say, then just specialize the template function. One for regular char and one for wchar_t and one for the rest, if need be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What about this?
    Code:
    if ( str[i] != static_cast<T>( 0 ) )

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You mean static_cast<T>('0')?

    I don't think the original code would work if there character was a wchar_t that when casted to char becomes '0'. So use _T() or I would imagine the static_cast would work for char and wchar_t.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need to do any cast when comparing a character (unless it's an extension by MS?). Comparing strings is an entirely different matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    So use _T()
    Great, but that only works in visual C++. Doesn't C++ itself have something liek this?
    You don't need to do any cast when comparing a character (unless it's an extension by MS?).
    Why? Comparing a string is just comparing a bunch of characters. If you can compare the characters then what makes a string so different?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Banana Man View Post
    Great, but that only works in visual C++. Doesn't C++ itself have something liek this?
    No, but that define depends on if you design uinicode or multibyte apps.
    Technically, it's defined as
    Code:
    #ifdef UNICODE
    #define _T L
    #else
    #define _T
    #endif
    So it's dependant on project settings and not the type T you pass to your template function.
    If this behavior is what you want, you can define it yourself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You don't need to do any cast when comparing a character (unless it's an extension by MS?). Comparing strings is an entirely different matter.

    The static_cast is potentially casting the '0' to wchar_t. Does the comparison without the cast do the same thing, or does it convert the character str[i] to a char? If it's converting str[i] to a char, then you would have a problem.

    The _T macro is a little more complicated than that I believe. It uses a parameter.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But as I mentioned, you don't have to do L:
    Code:
    	char c = 'c';
    	wchar_t w = 'w';
    	if (c == 'c') cout << "YES! c == c!\n";
    	if (w == 'w') cout << "YES! w == w!\n";
    Both ifs evaluate as true. So unless this is an MS extension, you don't need a cast at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    You mean static_cast<T>('0')?

    I don't think the original code would work if there character was a wchar_t that when casted to char becomes '0'. So use _T() or I would imagine the static_cast would work for char and wchar_t.
    Oops, I was mistaking '0' with '\0'.

    Quote Originally Posted by Elysia
    Technically, it's defined as

    Code:
    #ifdef UNICODE
    #define _T L
    #else
    #define _T
    #endif
    Actually, wouldn't it be more like this:
    Code:
    #ifdef UNICODE
    #define _T( x )   L ## x
    #else
    #define _T( x )   x
    #endif

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Elysia View Post
    Both ifs evaluate as true. So unless this is an MS extension, you don't need a cast at all.
    Both char and wchar_t are integeral types. There is never a cast needed to compare them. At best you will get a warning if the comparison might not work as expected (e.g. signed <> unsigned ).
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Comparing characters
    By Thuz in forum C Programming
    Replies: 2
    Last Post: 09-16-2007, 12:07 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Comparing Characters
    By luckygold6 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2003, 08:19 PM