消除警告的最佳方法? [英] Best way to eliminate warnings ?
问题描述
我有几种采用任何整数类型的模板方法,
第一个if当T是一个
无符号类型时,语句成为一个常量表达式。
#include< limits>
template< typename T>
静态T bin_to_gray(const T& value)
{
if(value> = 0)/ /<<警告 - allways true当T签名时
{
返回值^(值>> 1);
}
else
{
return((〜值)^((〜值)>> 1))
+ std :: numeric_limits< T> :: min();
}
}
任何消除警告的便携方式?
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 ?
推荐答案
2003年12月2日21:53:34 EST,Gianni Mariani< gi ******* @ mariani.ws>写道:
On 02 Dec 2003 21:53:34 EST, Gianni Mariani <gi*******@mariani.ws> wrote:
我有几个模板方法,但是,第一个if当T是无符号类型时,语句成为常量表达式。
#include< limits>
模板< typename T>
静态T bin_to_gray(const T& value)
{
if(value> = 0)//<<警告 - allways true当T签名时
{
返回值^(值>>>&n;);
}
{
返回((〜值)^ ((〜值)>> 1))
+ std :: numeric_limits< T> :: min();
}
}
任何便携式消除警告的方法?
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 ?
else-block中的表达式不可移植,因此可移植性没有问题
问题。 />
但是,你可以使用std :: numeric_limits< T> :: is_signed来为你的签名类型移植专门的
函数。
例如(关闭袖口),
模板< bool,typename T1,typename T2> struct Choice;
template< typename T1,typename T2> struct Choice< true,T1,T2> {typedef T1 T; };
模板< 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值)...
};
template<> struct GrayConverter< UnsignedType> ...
模板< typename T>
静态内联T bin_to_gray(T值)
{
返回GrayConverter< Signedness< T> :: Type> :: to_gray(value);
}
Choice机制用于更可读的GrayConverter定义;你或者b $ b可以直接用一个bool值来模糊GrayConverter。
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.
2003年12月2日21:53:34 EST,Gianni Mariani< gi ******* @ mariani.ws>
写道:
On 02 Dec 2003 21:53:34 EST, Gianni Mariani <gi*******@mariani.ws>
wrote:
我有几个模板方法,然而,取任何整数类型,
第一个if当T是无符号类型时,语句成为常量表达式。
#include< limits>
模板< typename T>
静态T bin_to_gray(const T& value)
{
if(value> = 0)//<<警告 - allways true当T签名时
{
返回值^(值>>>&n;);
}
{
返回((〜值)^ ((〜值)>> 1))
+ std :: numeric_limits< T> :: min();
}
}
任何便携式消除警告的方法?
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 ?
如何专业化未签名的
模板<>
static unsigned long
bin_to_gray< unsigned long>(unsigned long const& val)
{
return val ^(val>> 1);
}
模板<>
静态无符号短
bin_to_gray< unsigned short>( unsigned short const& val)
{
返回val ^(val>> 1)
}
>
等等对于unsigned char / long long
我肯定使用数字特性会更好,但我还没有
我以前使用过的特质。
>
干杯!
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!
" Gianni Mariani" < GI ******* @ mariani.ws>在消息中写道
news:bq ******** @ dispatch.concentric.net ...
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bq********@dispatch.concentric.net...
我有几个模板方法然而,取任何整数
类型,第一个if当T是无符号类型时,语句变为常量表达式
。
#include< limits>
模板< typename T>
静态T bin_to_gray(const T& value)
{
if(value> = 0)//<<警告 - allways true $ t $ b当T unsigned {
返回值^(值>>>>>>>>
)
返回时((〜值)^ ((〜值)>> 1))
+ std :: numeric_limits< T> :: min();
}
}
任何便携式消除警告的方法?
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 ?
怎么样
模板< typename T>
static T bin_to_gray(const T& value)
{
if(!std :: numeric_limits< T> :: is_signed || value> = 0)
//
{
返回值^(值>> 1);
}
其他
{
返回((〜值)^((〜值)>> 1))
+ std :: numeric_limits< T> :: min();
}
}
- -
祝你好运,John E.
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.
这篇关于消除警告的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!