templates question

M

Mark P

I put together the following little template meta-foolishness in order
to quiet some compiler warning about "pointless comparison of unsigned
integer with zero" for various unsigned types. As you can see, I made
separate template parameters for isSigned and isSpecialized (since the
unspecialized value of is_signed is false).

Now I'm just wondering if it's permitted to combine these as a single
parameter, say isSpecializedAndUnsigned, and in the definition of
NONNEGATIVE write something like:

return NonNegativeCore<T,std::numeric_limits<T>::is_specialized &
!std::numeric_limits<T>::is_signed>;

In other words, can I always rely upon the compiler to do the simple
boolean algebra above (it works for me with gcc)? And more generally,
what sorts of expressions will the compiler evaluate in this situation?

Thanks,
Mark

// code sinppet

template <typename T, bool isSigned, bool isSpecialized>
struct NonNegativeCore
{
static bool isIt (T x) {return x >= 0;}
};

template <typename T>
struct NonNegativeCore<T,false,true>
{
static bool isIt (T x) {return true;}
};

template <typename T>
inline bool NONNEGATIVE (T x)
{
return NonNegativeCore<T,std::numeric_limits<T>::is_signed,
std::numeric_limits<T>::is_specialized>::isIt(x);
}
 
A

andy

Mark said:
I put together the following little template meta-foolishness in order
to quiet some compiler warning about "pointless comparison of unsigned
integer with zero" for various unsigned types. As you can see, I made
separate template parameters for isSigned and isSpecialized (since the
unspecialized value of is_signed is false).

Now I'm just wondering if it's permitted to combine these as a single
parameter, say isSpecializedAndUnsigned, and in the definition of
NONNEGATIVE write something like:

return NonNegativeCore<T,std::numeric_limits<T>::is_specialized &
!std::numeric_limits<T>::is_signed>;

Yes That is fine. You might use && instead of & for style but AFAICS
it really makes little difference in this case.
In other words, can I always rely upon the compiler to do the simple
boolean algebra above (it works for me with gcc)?

Yes. A conforming compiler will do compile time-math on any integral
constant expression. For more on integral constant expression see
below.

And more generally,
what sorts of expressions will the compiler evaluate in this situation?

See http://www.boost.org/more/int_const_guidelines.htm. The article is
somewhat boost related and is very conservative as it uses workarounds
for old and broken compilers, which you may or may not need depending
on your target audience. GCC is very good at implementing integral
constant expression's sometimes called (ICE's) though AFAIK.

HTH

regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top