Restricting template parameters to signed or unsigned types, but notboth

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Damian

    Restricting template parameters to signed or unsigned types, but notboth

    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 only be
    instantiated with unsigned types.

    template <typename unsigned T>
    inline T id(T *i) {
    return i;
    }

    void foo() {
    (void)id<int>(k ); // should return an error.
    (void)id<unsign ed int>(k); // should be fine.
    }

    Does anyone know how I can achieve this?

    Thanks!

    Best Regards,

    Damian Eads
  • Ron Natalie

    #2
    Re: Restricting template parameters to signed or unsigned types,but not both

    Damian wrote:
    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 only be
    instantiated with unsigned types.
    >
    template <typename unsigned T>
    inline T id(T *i) {
    return i;
    }
    >
    void foo() {
    (void)id<int>(k ); // should return an error.
    (void)id<unsign ed int>(k); // should be fine.
    }
    >
    I don't know what algorithm you are indending to implement (your
    example is contrived and invalid).

    The general way is to just accept unqualified T but apply a

    Comment

    Working...