C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defining func_obj_traits similar to iterator_traits

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Prateek R Karandikar
Guest





PostPosted: Wed Apr 28, 2004 7:17 pm    Post subject: Defining func_obj_traits similar to iterator_traits Reply with quote



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 ]

Back to top
Prateek R Karandikar
Guest





PostPosted: Mon May 10, 2004 7:35 am    Post subject: Re: Defining func_obj_traits similar to iterator_traits Reply with quote



[email]kprateek88 (AT) yahoo (DOT) com[/email] (Prateek R Karandikar) wrote in message
Quote:
... 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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.