473,406 Members | 2,371 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,406 software developers and data experts.

templates question

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_speci alized &
!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_signe d,
std::numeric_limits<T>::is_specialized>::isIt(x);
}
Apr 27 '06 #1
1 2130

Mark P wrote:
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_speci alized &
!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

Apr 27 '06 #2

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
2
by: Kimmo | last post by:
Folks, I have a XML document that has been put together from a "dynamic part" (generated somehow during runtime) and a "static part" (read from a control file). Basically the document looks like...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
2
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
0
by: Piper707 | last post by:
I need help with using a general template which would process all tags other than the ones for which specific templates have been written. My XML looks like this: <ITEMS>...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
0
by: =?Utf-8?B?R3V5?= | last post by:
I would like developing an application for sending e-mail based on a (html) template. Scenario: user chooses distribution list and chooses a template (fro ma list of available html-templates)....
2
by: madhu.srikkanth | last post by:
Hi, I came across a paper by Angelika Langer in C++ Users Journal on Expression Templates. In the article she had mentioned that the code snippet below used to calculate a dot product is an...
26
by: puzzlecracker | last post by:
Team, C++ has been around since 1986, why templates are still regarded is a new feature by most compiler vendors and not fully supported (for example export feature). Look at other popular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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...

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.