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

Signed and unsigned template type parameters

Hello,

Consider the member function length() in the following declaration:

enum range_direction { NONE, FORWARD, REVERSE };

template<typename E, typename D>
class Range
{
typedef E element_type;
typedef D distance_type;
public:
Range();
Range( const E & startT
, const E & endT
, bool directed = true);
Range( const E & startT
, const D & lengthT
, bool directed
, range_direction direction);
Range( const Range & );
Range& operator=( const Range & );
~Range() { ; }

const E & begin() const { return m_start; }
const E & end() const { return m_end; }
D length() const { return (m_end - m_start); }
// etc.
private:
E m_start;
E m_end;
bool m_directed;
};

How can I prohibit template instantiations with distance_type of
unsigned integral types? E.g. Range<double, unsigned int>?

It can be OK if the Range has no implicit direction; however, if
REVERSE is specified, then (m_end - m_start) should reflect that.

Of course, for user-defined types, it is up to the application to
define D such that E(endElem) - E(startElem) reflects the direction in
the D which is returned (or pass direction=false to the appropriate
constructor).

But is it possible to prevent unsigned D for built-in integral types?
Do I need to create template specializations for each type (i.e.
unsigned char, unsigned short, unsigned int, etc.)? I could do that
and throw an exception, but it would be nicer to have a compile-time
check.

Thanks.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #1
1 1995

"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:2r********************************@4ax.com...
Hello,

Consider the member function length() in the following declaration:

enum range_direction { NONE, FORWARD, REVERSE };

template<typename E, typename D>
class Range
{
typedef E element_type;
typedef D distance_type;
public:
Range();
Range( const E & startT
, const E & endT
, bool directed = true);
Range( const E & startT
, const D & lengthT
, bool directed
, range_direction direction);
Range( const Range & );
Range& operator=( const Range & );
~Range() { ; }

const E & begin() const { return m_start; }
const E & end() const { return m_end; }
D length() const { return (m_end - m_start); }
// etc.
private:
E m_start;
E m_end;
bool m_directed;
};

How can I prohibit template instantiations with distance_type of
unsigned integral types? E.g. Range<double, unsigned int>?


I would use the boost concept check library

#include <boost/concept_check.hpp>

template<typename E, typename D>
class Range
{
BOOST_CLASS_REQUIRE(D, boost, SignedIntegerConcept);

...

john
Jul 22 '05 #2

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

Similar topics

44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
2
by: REH | last post by:
If the is_modulo field of the numeric_limits class is true for signed integer types, can I assume that overflow for such types is defined behavior? If so, is the behavior the same regardless of...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
8
by: Marcin Kalicinski | last post by:
Are 3 types: signed char, char and unsigned char distinct? My compiler is treating char as signed char (i.e. it has sign, and range from -128 to 127), but the following code does not call f<char>...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
1
by: Damian | last post by:
Hi there, I would like to restrict a template parameter to a template functioned to either a signed or unsigned type, but not both. For example, the following templated function should ideally...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.