473,466 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to distinguish whether the type is unsigned or not

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
15 1668
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
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
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
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
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
Try this:
#define STATIC_ASSERT_IS_NOT_SIGNED(type) \
{ static const char someArray[(char)(type(-1) < type(1))]; }

Dec 22 '05 #7

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
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
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
"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

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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
6
by: mosfet | last post by:
Hi, how can i make the difference between a char* and a char because I need to do this void myfun(char * tab) { int nTmp; nTmp = sizeof (tab);
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
4
by: raj | last post by:
Hi, I am a beginner and need help with the following: 'ifr_data' is (char *) 'args' is unsigned long args ((unsigned long *)(&ifr.ifr_data)) = (unsigned long)args; What does the...
2
by: Laszlo Szijarto | last post by:
Using reflection, I am iterating through an array of FieldInfo objects and wish to determine whether any given field represents a signed or an unsigned variable (makes a difference in terms of how...
10
by: Toms | last post by:
When you simply want to store a number, what integral type do you use? For instance, let's say we have the following in a Poker game: struct Card { enum Suit { Hearts, Diamonds, Spades, Clubs...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
10
by: QQ | last post by:
for instance, I read a char from the input and I need to decide whether it is a letter or a number What I am doing is char a; ...... // read a int true = false; if(( (a >='0') && (a <='9')) | |...
3
by: Arndt Jonasson | last post by:
Let's say we have a schema (maybe expressed in XML Schema, but not necessarily so), that allows this instance document: <top> <txt>This is text</txt> <books> <book>Tarzan</book> <book>Harry...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.