Restricting template parameters to signed or unsigned types, but notboth

Discussion in 'C++' started by Damian, Apr 7, 2008.

  1. Damian

    Damian Guest

    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<unsigned int>(k); // should be fine.
    }

    Does anyone know how I can achieve this?

    Thanks!

    Best Regards,

    Damian Eads
    Damian, Apr 7, 2008
    #1
    1. Advertising

  2. Damian

    Damian Guest

    Re: Restricting template parameters to signed or unsigned types, butnot both

    On Apr 7, 10:10 am, Ron Natalie <> wrote:
    > 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<unsigned int>(k); // should be fine.
    > > }

    >
    > I don't know what algorithm you are indending to implement (your
    > example is contrived and invalid).


    Err, I meant

    template class <typename T> // How do I reflect the signedness or
    unsignedness restriction here?
    inline T id(T i) {
    return i;
    }

    It is contrived. I don't think it's important for you to see the
    algorithm. I want to know how to restrict the signedness of templates
    in the general case. I have found that when I post specifics about the
    algorithm, people focus their comments on the algorithm, when I'm
    interested in entirely something else.

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


    I didn't quite get that. Please resend.

    Thanks,

    Damian
    Damian, Apr 7, 2008
    #2
    1. Advertising

  3. Damian

    Damian Guest

    Re: Restricting template parameters to signed or unsigned types, butnot both

    On Apr 7, 1:18 pm, "Victor Bazarov" <> wrote:
    > Damian wrote:
    > > On Apr 7, 10:10 am, Ron Natalie <> wrote:
    > >> 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<unsigned int>(k); // should be fine.
    > >>> }

    >
    > >> I don't know what algorithm you are indending to implement (your
    > >> example is contrived and invalid).

    >
    > > Err, I meant

    >
    > > template class <typename T> // How do I reflect the signedness or
    > > unsignedness restriction here?
    > > inline T id(T i) {
    > > return i;
    > > }

    >
    > > It is contrived. I don't think it's important for you to see the
    > > algorithm. I want to know how to restrict the signedness of templates
    > > in the general case. I have found that when I post specifics about the
    > > algorithm, people focus their comments on the algorithm, when I'm
    > > interested in entirely something else.

    >
    > Often enough we see folks asking "how do I <whatever>" thinking that
    > it would solve some problem they allegedly have, while the actual
    > solution to what's holding them from an acceptable result is nothing
    > close. That is why sometimes, especially if the solution isn't at
    > all an easy one, we might need proof (to ourselves) that we're not
    > spending time solving the problem that you don't actually have.
    >
    > There is nothing personal here, please understand.


    Not taking anything personally. I understand list traffic frequently
    deals with questions about algorithms. However, while I am sure many
    of this groups' members are quite skilled in algorithms, it is not
    typically what I would come to a C++ list for.

    Really what I am interested in how to restrict template arguments on
    signedness as well as base class. It looks like boost has some
    sophisticated template tools so I will look into them more deeply.
    Thanks for bringing them to my attention.

    > As to your particular inquiry, you should probably look into Boost's
    > "enable_if" template and type traits, so you could "enable if your
    > type T has a trait of being of the unsigned [integral] variety".


    Thanks. I will look into enable_if construct in Boost.

    Damian
    Damian, Apr 7, 2008
    #3
    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. Bob Hairgrove
    Replies:
    1
    Views:
    389
    John Harrison
    Jun 26, 2004
  2. Jason Heyes
    Replies:
    2
    Views:
    583
    Jason Heyes
    Feb 14, 2005
  3. Guest
    Replies:
    0
    Views:
    359
    Guest
    Dec 20, 2006
  4. Ben Finney
    Replies:
    11
    Views:
    481
    Dan Bishop
    Dec 24, 2006
  5. pozz
    Replies:
    12
    Views:
    718
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page