Topic: Extended the functional templates in the standard library
Author: Brian Michael Freyburger <freyburg@gamut.stanford.edu>
Date: 1996/09/04 Raw View
Now that there are default template parameter values, has any thought
been given to extending the functional templates provided by the
standard library.
For example, right now we have:
template <class T> struct plus : binary_function<T,T,T> {
T operator()(const T& x, const T& y) const
{ return x + y; }
};
in the library. What about instead having:
template <class T1, class T2 = T1, class T3 = T1> struct plus : binary_function<T1,T2,T3> {
T3 operator()(const T1& x, const T2& y) const
{ return x + y; }
};
And the same for all of the provided functionals (minus, times,
divides, etc.)
This is very useful in cases where classes define the operators on
argument types other than themselves (or overload them).
Brian Freyburger
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: fwai@armltd.co.uk (Francis Wai)
Date: 1996/09/05 Raw View
Brian Michael Freyburger <freyburg@gamut.stanford.edu> writes:
>Now that there are default template parameter values, has any thought
>been given to extending the functional templates provided by the
>standard library.
>For example, right now we have:
>template <class T> struct plus : binary_function<T,T,T> {
> T operator()(const T& x, const T& y) const
> { return x + y; }
> };
>in the library. What about instead having:
>template <class T1, class T2 = T1, class T3 = T1>
> struct plus : binary_function<T1,T2,T3> {
> T3 operator()(const T1& x, const T2& y) const
> { return x + y; }
> };
Whether or not this requires extension depends on how a template
type parameter such as "template <class T> ..." is interpreted.
Despite similarity to the formal scope declaration e.g.
"void f(class T)", a template type parameter essentially names
an unknown type. Yes, pardon the parlance, its 'TYPE type' but
without runtime implications.
A template type parameter <class T> takes on the normal C++
class sense only if it has been further declared as such, e.g.
template <class T> struct X { friend class T; };
The examples in 14.2.1 in the DWP Oct. 95 illustrate this.
It would be illegal to instantiate, say "X<int> x;".
Further, the same DWP (14.7) also allows,
template <class T, T p> struct X {};
If your compiler chooses this interpretation, and supports type
dependency (as demanded by the DWP) among template type (and non-type)
parameters, your example ought to be legal.
--Francis
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Brian Michael Freyburger <freyburg@gamut.stanford.edu>
Date: 1996/09/05 Raw View
fwai@armltd.co.uk (Francis Wai) writes:
> Brian Michael Freyburger <freyburg@gamut.stanford.edu> writes:
>
> >Now that there are default template parameter values, has any thought
> >been given to extending the functional templates provided by the
> >standard library.
>
> >For example, right now we have:
>
> >template <class T> struct plus : binary_function<T,T,T> {
> > T operator()(const T& x, const T& y) const
> > { return x + y; }
> > };
>
> >in the library. What about instead having:
>
> >template <class T1, class T2 = T1, class T3 = T1>
> > struct plus : binary_function<T1,T2,T3> {
> > T3 operator()(const T1& x, const T2& y) const
> > { return x + y; }
> > };
>
> Whether or not this requires extension depends on how a template
> type parameter such as "template <class T> ..." is interpreted.
>
> [rest of message cut]
Unless I'm missing something, I don't think this addresses my
concern. I want to extend the definition of the standard template
classes plus, minus, times, etc. So that code like the following can
use them:
template <class T>
class RGB
{
RGB(T r, T g, T b) : r(r), g(g), b()
{}
RGB<T> operator *(const RGB<T> &a, const T &b)
{ return RGB<T>(a.r * b, a.b * b, a.b * b); }
private:
T r, g, b;
};
int main()
{
int in[] = {1, 2, 3, 4, 5};
vector<RGB<int> > out(5, RGB<int>(1, 1, 1));
transform(out, in, out, times<RGB<int>, int>());
}
Currently, this code work require you to define your own functional
class for the times. In my experience I have had to define several
such auxillary classes which could have been eliminated by the
extended definitions of plus, times, etc. [As well as a good set of
composition function, but that's another issue.]
Brian Freyburger
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]