Topic: Defining func_obj_traits similar to iterator_traits
Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Mon, 10 May 2004 07:35:49 +0000 (UTC) Raw View
kprateek88@yahoo.com (Prateek R Karandikar) wrote in message
> ... In contrast a real pointer
> (to an object) can be used as an iterator, because the size_type, etc
> typedefs aren't used directly( In::size_type), but through
> iterator_traits (iterator_traits<In>::size_type)...
Sorry, I meant value_type, difference_type, etc , not size_type.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Wed, 28 Apr 2004 13:17:18 CST Raw View
We can't pass a pointer to function to a function (that expects a
function object) that uses the result_type, argument_type, etc
typedefs, instead we have to use ptr_fun. In contrast a real pointer
(to an object) can be used as an iterator, because the size_type, etc
typedefs aren't used directly( In::size_type), but through
iterator_traits(iterator_traits<In>::size_type). An analogous approach
can be used for function objects:
template <typename Op>
struct func_obj1_traits //unary function
{
typedef typename Op::argument_type argument_type;
typedef typename Op::result_type result_type;
};
template <typename Arg, typename Res>
struct func_obj1_traits< Res(*)(Arg) > //specialisation for pointer
to unary function
{
typedef Arg argument_type;
typedef Res result_type;
};
template <typename BinOp>
struct func_obj2_traits //binary function
{
typedef typename BinOp::first_argument_type first_argument_type;
typedef typename BinOp::second_argument_type second_argument_type;
typedef typename BinOp::result_type result_type;
};
template <typename Arg1, typename Arg2, typename Res>
struct func_obj2_traits< Res(*)(Arg1, Arg2) > //specialisation for
pointer to binary function
{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Res result_type;
};
In a function using function objects, instead of using
Op::result_type, func_obj1_traits<Op>::result_type would be used.
Similarly for the other typedefs.
This would save the user from the burden of using ptr_fun and would be
as efficient.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]