public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/13295] New: for some integer types U, (~U(0)) < U(0) != U(~U(0)) < U(0)
@ 2003-12-04  5:53 gianni at mariani dot ws
  2003-12-04  8:12 ` [Bug c++/13295] " pinskia at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: gianni at mariani dot ws @ 2003-12-04  5:53 UTC (permalink / raw)
  To: gcc-bugs

It seems to be that constant expressions in templates involvoing ~T(0) for
unsigned char and unsigned short result in strange behaviour where:

(~U(0)) is not the same as U(~U(0))

see below:

Here is the test code results:

test for : signed char - OK
test for : char - OK
test for : short - OK
test for : int - OK
test for : long - OK
test for : long long - OK
test for : unsigned char - Failed
test for : unsigned short - Failed
test for : unsigned int - OK
test for : unsigned long - OK
test for : unsigned long long - OK

template <typename U>
struct type_tester
{
    // is_signed and is_signed_cast should theoretically be identical
    // ~U(0) and U(~U(0)) should be identical - for unsigned short
    // unsigned and char this seems not to be the case on gcc 3.3.1
    
    static const bool is_signed = (~U(0)) < U(0);
    static const bool is_signed_cast = U(~U(0)) < U(0);
};

//
// test code
#include <typeinfo>

template <typename U> struct type_namer
{ static const char * name; };

template <typename U>
const char * type_namer<U>::name = typeid(U).name();

// macro to create specialization
#define make_namer(A) \
template <> struct type_namer<A> \
{ static const char * name; }; \
template <> const char * type_namer<A>::name = #A; \
// end

make_namer(signed char)
make_namer(char)
make_namer(short)
make_namer(int)
make_namer(long)
make_namer(long long)
make_namer(unsigned char)
make_namer(unsigned short)
make_namer(unsigned int)
make_namer(unsigned long)
make_namer(unsigned long long)

#include <iostream>

//
// template function to run test
//
template <typename U>
void show()
{
    std::cout << "test for : " << type_namer<U>::name << " - ";

    std::cout << (
        ( type_tester<U>::is_signed != type_tester<U>::is_signed_cast )
        ? "Failed"
        : "OK"
    );
    
    std::cout << "\n";
}

//
// test all integer types.
//
int main()
{
    show<signed char>();
    show<char>();
    show<short>();
    show<int>();
    show<long>();
    show<long long>();
    show<unsigned char>();
    show<unsigned short>();
    show<unsigned int>();
    show<unsigned long>();
    show<unsigned long long>();
}

-- 
           Summary: for some integer types U, (~U(0)) < U(0) != U(~U(0)) <
                    U(0)
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gianni at mariani dot ws
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13295


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/13295] for some integer types U, (~U(0)) < U(0) != U(~U(0)) < U(0)
  2003-12-04  5:53 [Bug c++/13295] New: for some integer types U, (~U(0)) < U(0) != U(~U(0)) < U(0) gianni at mariani dot ws
@ 2003-12-04  8:12 ` pinskia at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2003-12-04  8:12 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2003-12-04 08:12 -------
Not a bug, (~((unsigned char)0)) gets promoted to an int (because unsigned char is smaller than 
unsigned int) in both C and C++.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13295


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-12-04  8:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-04  5:53 [Bug c++/13295] New: for some integer types U, (~U(0)) < U(0) != U(~U(0)) < U(0) gianni at mariani dot ws
2003-12-04  8:12 ` [Bug c++/13295] " pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).