Allowing only certain classes as template parameters

Discussion in 'C++' started by Helge Preuss, Jul 22, 2004.

  1. Helge Preuss

    Helge Preuss Guest

    I have a function template

    template <typename T> void f (std::vector<T> values, int max) {
    // ...
    if (std::accumulate (values.begin (), values.end (), T()) > max) {
    // ...
    }
    // ...
    }

    Because I use std::accumulate (), there are some restrictions on T:
    T must have an operator + () and an explicit default constructor. It
    also needs an operator int () so that the result of std::accumulate
    can be compared to max.

    (How) can I ensure that f is only called with types that meet these
    restrictions? I thought of defining an abstract base class which
    implements the needed functions and allowing only derived classes as
    template parameter. But I don't know how to express this. Is it
    possible at all?

    Helge
    Helge Preuss, Jul 22, 2004
    #1
    1. Advertising

  2. Helge Preuss

    JKop Guest

    Helge Preuss posted:


    > (How) can I ensure that f is only called with types that

    meet these
    > restrictions?


    The compiler does that for you. It simply won't compile if
    the necessary member function and operators aren't defined.

    Outside of that, just document that specific member
    functions and operators that must be defined.

    -JKop
    JKop, Jul 22, 2004
    #2
    1. Advertising

  3. Helge Preuss

    tom_usenet Guest

    On 22 Jul 2004 06:07:05 -0700, (Helge Preuss) wrote:

    >I have a function template
    >
    > template <typename T> void f (std::vector<T> values, int max) {
    > // ...
    > if (std::accumulate (values.begin (), values.end (), T()) > max) {
    > // ...
    > }
    > // ...
    > }
    >
    >Because I use std::accumulate (), there are some restrictions on T:
    >T must have an operator + () and an explicit default constructor. It
    >also needs an operator int () so that the result of std::accumulate
    >can be compared to max.
    >
    >(How) can I ensure that f is only called with types that meet these
    >restrictions? I thought of defining an abstract base class which
    >implements the needed functions and allowing only derived classes as
    >template parameter. But I don't know how to express this. Is it
    >possible at all?


    Obviously it won't compile if you pass something without the necessary
    functionality. But if you want clearer error messages, you can use
    "Concept Checks". See
    http://www.boost.org/libs/concept_check/concept_check.htm

    You can also use the enable_if library to eliminate "f" from overload
    resolution if certain criteria aren't met, but I think that's overkill
    for your example.

    Tom
    tom_usenet, Jul 22, 2004
    #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. Aaron

    Only allowing certain html tags

    Aaron, Jan 22, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    379
    Aaron
    Jan 22, 2006
  2. ian
    Replies:
    5
    Views:
    518
  3. Asfand Yar Qazi
    Replies:
    5
    Views:
    358
    Siemel Naran
    Apr 7, 2004
  4. David Cater
    Replies:
    6
    Views:
    1,756
    Pavel Lepin
    May 31, 2007
  5. kito
    Replies:
    2
    Views:
    406
Loading...

Share This Page