Signed and unsigned template type parameters

Discussion in 'C++' started by Bob Hairgrove, Jun 26, 2004.

  1. 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
    Bob Hairgrove, Jun 26, 2004
    #1
    1. Advertising

  2. "Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
    news:...
    > 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
    John Harrison, Jun 26, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rade
    Replies:
    8
    Views:
    479
  2. NM
    Replies:
    6
    Views:
    452
    Default User
    Sep 20, 2006
  3. Damian
    Replies:
    2
    Views:
    571
    Damian
    Apr 7, 2008
  4. pozz
    Replies:
    12
    Views:
    725
    Tim Rentsch
    Mar 20, 2011
  5. Replies:
    3
    Views:
    520
    Victor Bazarov
    Aug 1, 2012
Loading...

Share This Page