Re: [C++] Casting void* pointers within different scope

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 01/12/04


Date: Mon, 12 Jan 2004 23:51:20 +1100


"Robert W Hand" <rwhand@NOSPAMoperamail.com> wrote in message
news:bca300hulmoj85fp51dser8gt1tou2g6i9@4ax.com...
| On Sat, 10 Jan 2004 00:30:53 +1100, "Chris \( Val \)"
| <chrisval@bigpond.com.au> wrote:
|
| >| I'll look over your code later today when I have time.

Hi Bob.

| I had a chance today to look over your code. As I see it you are
| trying to use find_if by changing from the predicate style of finding
| an element to using a member function method. So you have wrapped
| std::find_if into your own templated find_if function. The predicate
| becomes an "implementation detail" that is a function object called
| Find. Find requires a key value, a pointer to member (function), and
| an object of the class type to perform its duty. From what I see it
| seems to work as you would want it.

Right :-).

| I would make a few comments. First, I would not publish it as being
| part of namespace std. I understand that you were experimenting, but
| you are not allowed to add to namespace std. Why not place it in your
| own namespace?

I was just fooling around, but I did mention that you should
probably use your own :-). I was not sure however, if it was
legal to do it, so thanks for clearing that up.

| Second, I would avoid the const iterator template parameters. I would
| just make it similar to std::find_if. Perhaps, something like this:
|
| template<class Iter, class KeyType, class PtrToFunc>
| inline Iter find_if(Iter Start, Iter End,
| const KeyType& Value, PtrToFunc FuncPtr );

Any particular reason to avoid using the 'const_iterator' ?

| Third, I would add some comments. Although the names were selected to
| be "self-describing", PtrToFunc is not quite right. You planned to
| pass a pointer to member (function).

Yes, good point - I will add them, but I didn't see the need
at the time of posting.

| Fourth, there is another way to approach this problem using
| std::find_if. You want to pass an object to a predicate such as
| std::equal_to, but to equate a value that is returned from a member
| function of that object.

Yes.

| One can imagine it as a function composition g(f(x)) where a class
| object x is passed to a member function of x and then the returned
| value of that member function is passed to g. It still involves
| writing a composition function object, but it might be more usable in
| other situations. For this case, something like this might work:
|
| // Unary function composition function object.
| // Makes g(f(t)).
| // Requires F and G to be function adapters.

[snip example code]

| // I know that you like straightforward code. ;-)
|
| Person person("Chris");
|
| fg1_helper(std::mem_fun_ref(&Person::name),
| std::ptr_fun(print))(person);
|
|
| fg2_helper(std::ptr_fun(Iam),
| std::ptr_fun(print))("Frank", " Tom");
|
|
| Prints:
|
| Working with Chris :-)
| Chris
| Frank Tom

Yes, I was aware of 'std::equal_to', 'std::mem_fun_ref' and
'std::bind2nd' as used in your example, but I purposely tried
to avoid using them, because I dislike the syntax you have to
use(well, if your using the std algorithms at least).

Additionally, they are not the most intuitive methods to use,
and it can take a lot of thinking(if your not fully up to
speed with their syntax(like me)), to actually come up with
what your looking for.

Anyway, thanks for taking the time to look over my sample,
and providing an alternative view - much appreciated.

Cheers.
Chris Val



Relevant Pages