473,322 Members | 1,401 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,322 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 2127

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.