Connecting Tech Pros Worldwide Help | Site Map

Allowing only certain classes as template parameters

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:45 PM
Helge Preuss
Guest
 
Posts: n/a
Default Allowing only certain classes as template parameters

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, 04:45 PM
JKop
Guest
 
Posts: n/a
Default 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, 04:45 PM
tom_usenet
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.