473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Best way to eliminate warnings ?


I have a couple of template methods that take any integer type however,
the first "if" statement becomes a constant expression when T is an
unsigned type.
#include <limits>

template <typename T>
static T bin_to_gray( const T & value )
{

if ( value >= 0 ) // << warning - "allways true" when T unsigned
{
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

Any portable way to eliminate the warning ?

Jul 22 '05 #1
11 1775
On 02 Dec 2003 21:53:34 EST, Gianni Mariani <gi*******@mariani.ws> wrote:

I have a couple of template methods that take any integer type however,
the first "if" statement becomes a constant expression when T is an
unsigned type.
#include <limits>

template <typename T>
static T bin_to_gray( const T & value )
{

if ( value >= 0 ) // << warning - "allways true" when T unsigned
{
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

Any portable way to eliminate the warning ?


The expression in the else-block is not portable, so portability is a moot
issue.

However, you can use std::numeric_limits<T>::is_signed to portably specialize
your function for signed types.

For example (off the cuff),
template< bool, typename T1, typename T2 > struct Choice;
template< typename T1, typename T2 > struct Choice<true, T1, T2>{ typedef T1 T; };
template< typename T1, typename T2 > struct Choice<false, T1, T2>{ typedef T2 T; };

struct SignedType{};
struct UnsignedType{};

template< typename T >
struct Signedness
{
enum{ isSigned = numeric_limits<T>::is_signed };
typedef Choice<isSigned, SignedType, UnsignedType>::T Type;
};

template< class SignednessType > struct GrayConverter;

template<> struct GrayConverter<SignedType>
{
template< typename T >
T to_gray( T value ) ...
};

template<> struct GrayConverter<UnsignedType> ...

template <typename T>
static inline T bin_to_gray( T value )
{
return GrayConverter<Signedness<T>::Type>::to_gray( value );
}
The Choice machinery is for a more readable definition of GrayConverter; you
could alternatively templatize GrayConverter directly on a bool value.

Jul 22 '05 #2
On 02 Dec 2003 21:53:34 EST, Gianni Mariani <gi*******@mariani.ws>
wrote:

I have a couple of template methods that take any integer type however,
the first "if" statement becomes a constant expression when T is an
unsigned type.
#include <limits>

template <typename T>
static T bin_to_gray( const T & value )
{

if ( value >= 0 ) // << warning - "allways true" when T unsigned
{
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

Any portable way to eliminate the warning ?

How about specializing for unsigned's

template <>
static unsigned long
bin_to_gray<unsigned long>(unsigned long const & val)
{
return val^(val>>1);
}
template <>
static unsigned short
bin_to_gray<unsigned short>(unsigned short const & val)
{
return val^(val>>1)
}

and so on for unsigned char/long long

I'm sure that using a numeric trait would be better, but I haven't
used traits myself before.

cheers!
Jul 22 '05 #3
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bq********@dispatch.concentric.net...

I have a couple of template methods that take any integer type however, the first "if" statement becomes a constant expression when T is an unsigned type.
#include <limits>

template <typename T>
static T bin_to_gray( const T & value )
{

if ( value >= 0 ) // << warning - "allways true" when T unsigned {
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

Any portable way to eliminate the warning ?


How about

template <typename T>
static T bin_to_gray( const T & value )
{

if ( !std::numeric_limits<T>::is_signed || value >= 0 )
//
{
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

- -
Best regards, John E.
Jul 22 '05 #4
Alf P. Steinbach wrote:
On 02 Dec 2003 21:53:34 EST, Gianni Mariani <gi*******@mariani.ws> wrote:

I have a couple of template methods that take any integer type however,
the first "if" statement becomes a constant expression when T is an
unsigned type.
#include <limits>

template <typename T>
static T bin_to_gray( const T & value )
{

if ( value >= 0 ) // << warning - "allways true" when T unsigned
{
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

Any portable way to eliminate the warning ?

The expression in the else-block is not portable, so portability is a moot
issue.


You mean 2's complement ? That's not as big an issue. Any suggestions ?

However, you can use std::numeric_limits<T>::is_signed to portably specialize
your function for signed types.

For example (off the cuff),


Yeah, I was afraid of this.

example snipped.

That's alot of code to fight warnings from *valid code* ... oh well.
The end result is here.

http://www.mariani.ws/~gianni/at_gray_code.h

Jul 22 '05 #5
John Ericson wrote:
....

Any portable way to eliminate the warning ?

How about

template <typename T>
static T bin_to_gray( const T & value )
{

if ( !std::numeric_limits<T>::is_signed || value >= 0 )
//
{
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

Yep - I tried that first too. I also tried

std::numeric_limits<T>::is_signed ? value >= 0 : true
I decided to go with the template verbage method

see:
http://www.mariani.ws/~gianni/at_gray_code.h

Jul 22 '05 #6
Gianni Mariani wrote:

I have a couple of template methods that take any integer type however,
the first "if" statement becomes a constant expression when T is an
unsigned type.

#include <limits>

template <typename T>
static T bin_to_gray( const T & value )
{

if ( value >= 0 ) // << warning - "allways true" when T unsigned
{
return value ^ ( value >> 1 );
}
else
{
return ( ( ~value ) ^ ( ( ~value ) >> 1 ) )
+ std::numeric_limits<T>::min();
}
}

Any portable way to eliminate the warning ?


Turn warnings off. There's nothing wrong with that code, and rewriting
it to satisfy some compiler writer's notion of good style is a waste of
time.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7
Pete Becker wrote:
Gianni Mariani wrote:

....

Any portable way to eliminate the warning ?

Turn warnings off. There's nothing wrong with that code, and rewriting
it to satisfy some compiler writer's notion of good style is a waste of
time.


Thanks, but that's not portable.

I'm on the contrary with that style comment, I think this particular
warning is important because it can catch errors.

Leaving warning on is also a problem because if you want to make use of
warning like this one, it will get lost in a sea of other warnings.


Jul 22 '05 #8
Gianni Mariani wrote:

Pete Becker wrote:
Gianni Mariani wrote: ...

Any portable way to eliminate the warning ?

Turn warnings off. There's nothing wrong with that code, and rewriting
it to satisfy some compiler writer's notion of good style is a waste of
time.


Thanks, but that's not portable.


Nope. And working around idiosyncratic warnings isn't portable, either:
what one compiler likes another won't. So you have a choice: fight with
your compilers or tell them to shut up.

I'm on the contrary with that style comment, I think this particular
warning is important because it can catch errors.
What errors did it catch in this code? What do you gain from the extra
time you spend working around this warning and the extra code that you
end up writing?

Leaving warning on is also a problem because if you want to make use of
warning like this one, it will get lost in a sea of other warnings.


So shut it off.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #9
Gianni Mariani wrote:
Pete Becker wrote:
Gianni Mariani wrote:


...

Any portable way to eliminate the warning ?


Turn warnings off. There's nothing wrong with that code, and rewriting
it to satisfy some compiler writer's notion of good style is a waste of
time.


Thanks, but that's not portable.

I'm on the contrary with that style comment, I think this particular
warning is important because it can catch errors.

Leaving warning on is also a problem because if you want to make use of
warning like this one, it will get lost in a sea of other warnings.


I'd agree with you.

Warnings like this are indicative of potential problems with
the code, which is why we tend to ratchet up the warning
level of the compilers we use and turn them into compilation
errors.

In some cases the code that is warned about is OK, but it is
still worth spending the time to remove it, particularly if
the warning is coming from stuff in a header file because it
is likely to be generated in every compilation unit that
includes the header. Personally I'd rather not have to deal
with such headers.
Jul 22 '05 #10
On 03 Dec 2003 11:30:06 EST, Gianni Mariani <gi*******@mariani.ws>
wrote:
Pete Becker wrote:
Gianni Mariani wrote:

...

Any portable way to eliminate the warning ?

Turn warnings off. There's nothing wrong with that code, and rewriting
it to satisfy some compiler writer's notion of good style is a waste of
time.


Thanks, but that's not portable.

I'm on the contrary with that style comment, I think this particular
warning is important because it can catch errors.

Leaving warning on is also a problem because if you want to make use of
warning like this one, it will get lost in a sea of other warnings.


I'm just curious about what didn't you like about the solution I
offered you, namely, to write template specializations for unsigned
types. You want it portable? You got it. You want it simple? You got
it.

Jul 22 '05 #11
Dan W. wrote:
....

I'm just curious about what didn't you like about the solution I
offered you, namely, to write template specializations for unsigned
types. You want it portable? You got it. You want it simple? You got
it.


It violates two of my rules :

"Don't artificially limit the utility of a class (library)".

By enumerating implementations for a particular set of signed or
unsigned types, you are limiting it's use. For example, if I had a
special compiler with 128 bit unsigned values, I would have to augment
my library. I ended up using std::numeric_limits<T> which is less of an
issue but definitly encroaches on the rule.

The tennets of templates is to be generic, I see the reason for
specialization is to remove special cases, not add them.

The other issue is repeated functionality. While this case may be moot,
I feel that repeating "return val^(val>>1)" N times for every unsigned
type if the kind of thing that end up hurting in the future. If I have
a chunk-o-code that I test as an implementation<unsigned short> I would
like to have high confidence that implementation<unsigned long long>
would do the right thing. I've seen too many occurrences of cut-n-paste
code causing untold problems that I fear to tread there. Call this the
"an instance of a functionality must exist in exactly one place" rule.
I break this rule more often that I should and I almost allways find
that I should have never done so. However, I often break this rule
inadvertently, after a high level design, you still have yet to discover
all the "functionality" and sometimes you find yourself implementing the
same function twice. The more experienced engineers I've met, find and
factorize this far sooner than the less experienced ones.

Yes, your code would work fine, it would just be harder to maintain and
we could argue forever on that.

G

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kylotan | last post by:
I have the following code: def IntToRandFloat(x): """Given a 32-bit integer, return a float in """ x = int(x) x = int(x << 13) ^ x return...
131
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately...
3
by: Nicolae Fieraru | last post by:
Hi All, I have a select query which is based on tblOne and tblTwo. The query selects some of the records in tblOne and tblTwo, based on a Where condition. What I need to do is to change the...
22
by: John Fisher | last post by:
void f(int p) { } Many (most?) compilers will report that p is unreferenced here. This may not be a problem as f may have to match some common prototype. Typically pointers to functions are...
29
by: Daniel Rudy | last post by:
Hello, Consider the following code fragment: fileptr = fopen(param->filename, "r"); if (fileptr == NULL) { error("digest.c: digest_file: Open File ", errno); return(-2); }
7
by: Alfonso Morra | last post by:
I cannot believe how convoluted it is to export function templates and class templates from a shared library (or DLL as it is called in the Windoze world). Micro$oft support for STL and...
13
by: Rex Mottram | last post by:
I'm using an API which does a lot of callbacks. In classic callback style, each routine provides a void * pointer to carry user-defined data. Sometimes, however, the user-defined pointer is not...
1
by: Robert Singer | last post by:
Platform: winXP, excel 2003 Python 2.5.2 XLWriter 0.4a3 (http://sourceforge.net/projects/pyxlwriter/) Is anyone here using this very nice package, for writing excel files? I'm using it on...
5
by: Conrad Lender | last post by:
I'd like to hear your opinions about the appropriate way to deal with non-critical errors that can occur in user-defined functions. For example, an application chooses to extend String.prototype...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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 project—planning, coding, testing,...

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.