By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

how to distinguish whether the type is unsigned or not

P: n/a
hi,

consider the follow code:

#ifdef USE_UNSIGNED_INT
typedef unsigned int size;
#else
typedef unsigned int size;
#endif

latter in the program:
size i = XXX;
for (; i >0 ; i++) //problematic, if the i is the unsigned it!!!
{
}

is there any static assert way to distinguish whether the type is
unsigned or not? such as :

size i = XXX;
STATIC_ASSERT_IS_NOT_SIGNED(size);
for (; i >0 ; i++) {
}
thanks

Dec 22 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
sorry, the code should be:
size i = XXX;
for (; i >=0 ; i++) //problematic, if the i is the unsigned it!!!
{ }

thanks

Dec 22 '05 #2

P: n/a
On 21 Dec 2005 19:34:04 -0800, "baibaichen" <ba********@gmail.com>
wrote in comp.lang.c++:
sorry, the code should be:
size i = XXX;
for (; i >=0 ; i++) //problematic, if the i is the unsigned it!!!
{ }

thanks


Assuming the value is in range:

for (int signed_i = i; i>= 0; i++)

But I question your original logic. Assuming 'i' is signed to start
with, are you planning on incrementing it until it overflows? That's
undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 22 '05 #3

P: n/a
oops!! my mistake!

sorry, the code should be:
size i = XXX;
for (; i >=0 ; i--) //problematic, if the i is the unsigned it!!!
{ }

not i++

Dec 22 '05 #4

P: n/a
I don't know if its in the standard, but you may use the following
underflow detection:

while(i >= 0)
{
....
if (i - 1 > i)
break; // underflow
else
--i;
}

BTW, if the type of "i" is defined by the preprocessor than there
should be a compilation warning for unsigned (in gcc its something like
"condition is allways true"), and you can have the preprocessor create
the correct code for you. If the type of "i" is defined by a template
parameter that is sometimes unsigned, than its a possible (again I'm
not sure its standard) solution.

Yuval.

Dec 22 '05 #5

P: n/a
On 21 Dec 2005 19:30:12 -0800 in comp.lang.c++, "baibaichen"
<ba********@gmail.com> wrote,
is there any static assert way to distinguish whether the type is
unsigned or not? such as :


This is not tough to do along the lines of the boost template
metaprogramming library... they supply BOOST_STATIC_ASSERT and e.g.
::boost::is_integral<T>::value which is a close example, but you
would have to extend it for signed and unsigned which they do not
quite cover.
http://www.boost.org

Dec 22 '05 #6

P: n/a
Try this:
#define STATIC_ASSERT_IS_NOT_SIGNED(type) \
{ static const char someArray[(char)(type(-1) < type(1))]; }

Dec 22 '05 #7

P: n/a

baibaichen 写道:
hi,

consider the follow code:

#ifdef USE_UNSIGNED_INT
typedef unsigned int size;
#else
typedef unsigned int size;
#endif

latter in the program:
size i = XXX;
for (; i >0 ; i++) //problematic, if the i is the unsigned it!!!
{
}

is there any static assert way to distinguish whether the type is
unsigned or not? such as :

size i = XXX;
STATIC_ASSERT_IS_NOT_SIGNED(size);
for (; i >0 ; i++) {
}
thanks


-----------------------------------------------------------------------------------------------------
It's interesting, because I just joined a discussion similar to this
problem, but I'm not very sure whether answers given are what you want:
#define STATIC_ASSERT_IS_NOT_SIGNED(i) assert((i>=0)&&(~i>0))
or
#define STATIC_ASSERT_IS_NOT_SIGNED(i) assert((i-i-1)>0)

Just try it.

Regards,
Luo Bin (China)

Dec 22 '05 #8

P: n/a
ddh
template <class T>
struct Dummy;

template<>
struct Dummy<unsigned long>
{}

template<>
struct Dummy<unsigned int>
{}

template<>
struct Dummy<unsigned short>
{}

template<>
struct Dummy<unsigned char>
{}
#define STATIC_ASSERT_IS_NOT_SIGNED(type) sizeof(Dummy<type>)


baibaichen wrote:
oops!! my mistake!

sorry, the code should be:
size i = XXX;
for (; i >=0 ; i--) //problematic, if the i is the unsigned it!!!
{ }

not i++


Dec 22 '05 #9

P: n/a
David Harmon <so****@netcom.com> writes:
On 21 Dec 2005 19:30:12 -0800 in comp.lang.c++, "baibaichen"
<ba********@gmail.com> wrote,
is there any static assert way to distinguish whether the type is
unsigned or not? such as :


This is not tough to do along the lines of the boost template
metaprogramming library... they supply BOOST_STATIC_ASSERT and e.g.
::boost::is_integral<T>::value which is a close example, but you
would have to extend it for signed and unsigned which they do not
quite cover.


But that's trivial:

#include <boost/static_assert.hpp>
#include <limits>
#include "something_that_typedefs_size.h"

BOOST_STATIC_ASSERT(std::numeric_limits<size>::is_ signed);

/Niklas Norrthon
Dec 22 '05 #10

P: n/a
"baibaichen" <ba********@gmail.com> writes:
hi,

consider the follow code:

#ifdef USE_UNSIGNED_INT
typedef unsigned int size;
#else
typedef unsigned int size;
#endif

latter in the program:
size i = XXX;
for (; i >0 ; i++) //problematic, if the i is the unsigned it!!!
{
}

is there any static assert way to distinguish whether the type is
unsigned or not? such as :


See the thread 'templates to generate compiler diagnostics' I
started last week. I had an example doing exactly what you
ask for. Someone mentioned BOOST_STATIC_ASSERT that did the
same thing, a bit more complicated than I did, but also
a bit more elegant.

/Niklas Norrthon

Dec 22 '05 #11

P: n/a

yu*****@gmail.com wrote:
I don't know if its in the standard, but you may use the following
underflow detection:

while(i >= 0)
{
...
if (i - 1 > i)
break; // underflow
else
--i;
}


Definitely not in the standard. int underflow is UB just like int
overflow.
A completely valid implementation may define INT_MIN-1 as INT_MIN
and turn this into an infinite loop.

HTH,
Michiel Salters

Dec 22 '05 #12

P: n/a

"baibaichen" <ba********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
hi,

consider the follow code:

#ifdef USE_UNSIGNED_INT
typedef unsigned int size;
#else
typedef unsigned int size;
#endif

latter in the program:
size i = XXX;
for (; i >0 ; i++) //problematic, if the i is the unsigned it!!!
{
}

is there any static assert way to distinguish whether the type is
unsigned or not? such as :

size i = XXX;
STATIC_ASSERT_IS_NOT_SIGNED(size);
for (; i >0 ; i++) {
}
thanks


if(std::numeric_limits<size>::is_signed)
std::cout << "signed\n";
else
std::cout << "unsigned\n";
'std::numeric_limits<>' is declared by <limits>

-Mike
Dec 22 '05 #13

P: n/a
On 22 Dec 2005 10:58:40 +0100 in comp.lang.c++, Niklas Norrthon
<do********@invalid.net> wrote,
BOOST_STATIC_ASSERT(std::numeric_limits<size>::is _signed);


Thanks. I forgot to even look for that, having been thrown off in
the past by numeric_limits<T>::max() etc. which unfortunately cannot
be used in a static assert because it is a function call.

(I do not understand why max(), min() could not have been constants,
but undoubtedly the standards committee had some reason vastly more
important than making them checkable at compile time.)
Dec 22 '05 #14

P: n/a
On 22 Dec 2005 01:19:09 -0800 in comp.lang.c++, "Robin"
<rb******@hotmail.com> wrote,
It's interesting, because I just joined a discussion similar to this
problem, but I'm not very sure whether answers given are what you want:
#define STATIC_ASSERT_IS_NOT_SIGNED(i) assert((i>=0)&&(~i>0))


But that has a runtime call to assert() and therefore cannot be a
static assert!

Dec 22 '05 #15

P: n/a
In message <11**********************@g44g2000cwa.googlegroups .com>,
baibaichen <ba********@gmail.com> writes
oops!! my mistake!

sorry, the code should be:
size i = XXX;
for (; i >=0 ; i--) //problematic, if the i is the unsigned it!!!
{ }


Sorry to resurrect an old thread, but I think you're asking (and other
posters have answered) the wrong question. What I think you really want
is not "how do I distinguish between signed and unsigned?" but "how do I
write a loop that counts down to 0 inclusive, regardless of whether the
type is signed?"

Try this:

for (size i = something.size(); i-- > 0; /*nothing*/)
{
}

(note that what I've written as something.size() is equivalent to your
XXX + 1)

--
Richard Herring
Jan 13 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.