H
Helge Preuss
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
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