Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Signed and unsigned template type parameters

Reply
Thread Tools

Signed and unsigned template type parameters

 
 
Bob Hairgrove
Guest
Posts: n/a
 
      06-26-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-26-2004

"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class template specialization with signed/unsigned type problem andreas.schniertshauer@googlemail.com C++ 3 08-01-2012 03:03 PM
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Restricting template parameters to signed or unsigned types, but notboth Damian C++ 2 04-07-2008 08:45 PM
What happens when type conversion between signed and unsigned happens? NM C++ 6 09-20-2006 05:39 PM



Advertisments