Connecting Tech Pros Worldwide Help | Site Map

Allowing only certain classes as template parameters

  #1  
Old July 22nd, 2005, 05:45 PM
Helge Preuss
Guest
 
Posts: n/a
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
  #2  
Old July 22nd, 2005, 05:45 PM
JKop
Guest
 
Posts: n/a

re: Allowing only certain classes as template parameters


Helge Preuss posted:

[color=blue]
> (How) can I ensure that f is only called with types that[/color]
meet these[color=blue]
> restrictions?[/color]

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
  #3  
Old July 22nd, 2005, 05:45 PM
tom_usenet
Guest
 
Posts: n/a

re: Allowing only certain classes as template parameters


On 22 Jul 2004 06:07:05 -0700, gjong@gmx.net (Helge Preuss) wrote:
[color=blue]
>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?[/color]

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_ch...cept_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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax suggestion for concepts W Karas answers 20 March 21st, 2007 06:05 PM
Asp.net Important Topics. shamirza answers 0 January 18th, 2007 05:15 AM
Another C# critique christopher diggins answers 188 November 15th, 2005 09:13 PM
python-dev Summary for 2004-08-01 through 2004-08-15 Brett Cannon answers 17 July 18th, 2005 03:27 PM