J
jsnX
say i have a function object silly that takes a const ref to clowns
class silly : public std::unary_function<const clown&, bool>
{
...
}
and then i decide to feed it a bunch of pointers to clowns:
for_each(clown_pointers.begin(), clown_pointers.end(), silly());
of course this doesn't work. i would like some magic template that
makes it work like this:
for_each(clown_pointers.begin(), clown_pointers.end(),
pointer_protector(silly()));
so actually pointer_protector would be an overloaded function template,
which knows to check for whether or not it's getting a
std::unary_function<> or a std::binary_function<>, and then it would
spit out a corresponding unary pointer protector or binary pointer
protector. for the rest of this discussion, i will stick t talking
about the unary pointer protector and leave the binary pointer
protector as an exercise. here is my unary pointer protector template:
template<typename Arg, typename Res>
class unary : public std::unary_function<Arg*, Res>
{
public:
unary(const std::unary_function<Arg, Res>& f) : f(f)
{}
Res
operator() (Arg* a) //27
{ //28
return f(*a); //29
} //30
//31
private:
std::unary_function<Arg, Res> f;
};
this does not work - when i create a function object and encapsulate in
a pointer protector like this:
struct print_it : public std::unary_function<int, int>
{
...
}
int
main()
{
for_each( foo.begin(), foo.end(),
unary<print_it::argument_type,
print_it::result_type>(print_it()) );
}
i get an error like this:
unary.cpp:29 error: no match for call to `(unary_function<int, int>)
(int &)'
using g++ via gfilt on FreeBSD 5.4-STABLE. so what is the right way to
write a pointer to reference encapsulating translator? does it already
exist?
class silly : public std::unary_function<const clown&, bool>
{
...
}
and then i decide to feed it a bunch of pointers to clowns:
for_each(clown_pointers.begin(), clown_pointers.end(), silly());
of course this doesn't work. i would like some magic template that
makes it work like this:
for_each(clown_pointers.begin(), clown_pointers.end(),
pointer_protector(silly()));
so actually pointer_protector would be an overloaded function template,
which knows to check for whether or not it's getting a
std::unary_function<> or a std::binary_function<>, and then it would
spit out a corresponding unary pointer protector or binary pointer
protector. for the rest of this discussion, i will stick t talking
about the unary pointer protector and leave the binary pointer
protector as an exercise. here is my unary pointer protector template:
template<typename Arg, typename Res>
class unary : public std::unary_function<Arg*, Res>
{
public:
unary(const std::unary_function<Arg, Res>& f) : f(f)
{}
Res
operator() (Arg* a) //27
{ //28
return f(*a); //29
} //30
//31
private:
std::unary_function<Arg, Res> f;
};
this does not work - when i create a function object and encapsulate in
a pointer protector like this:
struct print_it : public std::unary_function<int, int>
{
...
}
int
main()
{
for_each( foo.begin(), foo.end(),
unary<print_it::argument_type,
print_it::result_type>(print_it()) );
}
i get an error like this:
unary.cpp:29 error: no match for call to `(unary_function<int, int>)
(int &)'
using g++ via gfilt on FreeBSD 5.4-STABLE. so what is the right way to
write a pointer to reference encapsulating translator? does it already
exist?