Functors vs. Function Pointers
Well, as more or less a joke, I am creating an implementation of the variables joke language. Variables is a Forth game orientated dialect, which attempts to improve on string parsing, while adding networking and ncurses abilities (plus whatever else I feel like). I have been working on documenting it, but the documentation is so behind the coding that it isn''t funny. It is behind, as the code right now is quite volatile, and I would not try to compile anything in the pages I have given in this post.
A Moderator in the Lounge, told me that functors would be a better way of doing the Dictionary Definitions, however, I am little unsure of what functors are, exactly, or what qualifies something as a functor. However, I am unsure of how a functor would work with the current version of how items are stored in the variables dictionary. I know that something like dictionary::add_verb(char *name, base_functor denotation), would not work, as you cannot pass by base class, as that is bad C++.
Any pointers on doing the moderator''s suggest of functors?
Thank you for your time.
-=-
"Boy? Girl? It does not matter, all that matters is power, and beauty is power. I am beautiful, so I have power and that is all that matters."
Got Immortality?
quote:Original post by DakeDesuTried a reference? It''s valid by pointer.
...you cannot pass by base class, as that is bad C++.
You can say my name, btw. No need for this "the moderator" stuff.
I''d recommend not writing documentation yet. Write a brief overview of the design (which can always change) and use automatic tools to generate documentation from the code and comments.
You can store base class pointers or you could make an envelope class which is stored by value and contains a pointer to the relevant function/functor.
There are plenty of resources about functors. Try searching for ''function object'' on boost.org or sgi.com using google to get ideas of how they are used.
Here are two links:
sgi''s stl documentation : functors
boost''s lambda documentation
You can store base class pointers or you could make an envelope class which is stored by value and contains a pointer to the relevant function/functor.
There are plenty of resources about functors. Try searching for ''function object'' on boost.org or sgi.com using google to get ideas of how they are used.
Here are two links:
sgi''s stl documentation : functors
boost''s lambda documentation
@Oluseyi, the "the moderator" stuff was mostly so I would not screw up and mispell your name
Seems to be what I was thinking it was, but the major problem with implementing it is the following:
I am refering to Forth dictionaries as Forth is the language this dialect is based off of. In my implementation, what happens is, the program is broken up by whitespace, and every thing inbetween the whitespace is checked to see if it is in the dictionary, or rather the area of code that the functions are stored, if not, the portion of code in question is put onto the stack. Generally, I check to see if something is in the dictionary via:
However, I may try passing it by reference, but when doing so, does it cause the functor to use the base class'' functionality (as it is passed as the base class), or does the compiler somehow know to do the subclass'' behavour?
Checked boost.org, as well as googled "function pointers," and found some marvelously done material on how to use functors. However, I also checked out this site''s resources and found next to nothing on functors--which I find just a little weird.
Thank you--I might post a followup
-=-
"Boy? Girl? It does not matter, all that matters is power, and beauty is power. I am beautiful, so I have power and that is all that matters."
Got Immortality?
quote:Oluseyi said this in the original thread that brought this upquote:Original post by DakeDesu
So, just to make sure I understand, what I am to do is make a base class, then have subclasses overload the () operator? I think I will look into it .That''s the essentials for polymorphic functor dispatch.struct MyFunctorBase{ virtual void operator () ( type1 t1, type2 t2 ) = 0; virtual ~MyFunctor() {}}; struct MyFunctorDerived : MyFunctorBase{ virtual void operator () ( type1 t1, type2 t2 ) { // blah }};
Seems to be what I was thinking it was, but the major problem with implementing it is the following:
quote:quote:The only problem is, how would I group them into one or two dictionaries?How do you mean? In C++ dictionaries are maps, so you''d use a pointer to the base functor class as either the key or value - mostly likely the value.
I am refering to Forth dictionaries as Forth is the language this dialect is based off of. In my implementation, what happens is, the program is broken up by whitespace, and every thing inbetween the whitespace is checked to see if it is in the dictionary, or rather the area of code that the functions are stored, if not, the portion of code in question is put onto the stack. Generally, I check to see if something is in the dictionary via:
dictionary local_dict; local_dict->is_word(<word>;
However, I may try passing it by reference, but when doing so, does it cause the functor to use the base class'' functionality (as it is passed as the base class), or does the compiler somehow know to do the subclass'' behavour?
quote:petewood
There are plenty of resources about functors. Try searching for ''function object'' on boost.org or sgi.com using google to get ideas of how they are used.
Checked boost.org, as well as googled "function pointers," and found some marvelously done material on how to use functors. However, I also checked out this site''s resources and found next to nothing on functors--which I find just a little weird.
Thank you--I might post a followup
-=-
"Boy? Girl? It does not matter, all that matters is power, and beauty is power. I am beautiful, so I have power and that is all that matters."
Got Immortality?
Looked at some things on Functors, and now I am even more confused.
From what I''ve seen, using Functors is more convoluted than using function pointers with all the X::Return_type, operator()() and the like needing to be done to make a decent Functor. I''ve noticed that a good portion of the examples I have seen, even use function pointers ontop of functors.
However, in my implementation of variables, all I really need to pass the function is the stack, and maybe an associated object for particularly weird functions. The return type is almost always the same--how much to get rid of the stack created on the first pass, as the second pass has used it.
Is there performance differences based on function pointers vs. functors? Is it mainly to make people not as confused while reading C++ code? Why would I want a Functor instead of a function pointer for my purposes, or even general purposes in a program?
-=-
"Boy? Girl? It does not matter, all that matters is power, and beauty is power. I am beautiful, so I have power and that is all that matters."
Got Immortality?
From what I''ve seen, using Functors is more convoluted than using function pointers with all the X::Return_type, operator()() and the like needing to be done to make a decent Functor. I''ve noticed that a good portion of the examples I have seen, even use function pointers ontop of functors.
However, in my implementation of variables, all I really need to pass the function is the stack, and maybe an associated object for particularly weird functions. The return type is almost always the same--how much to get rid of the stack created on the first pass, as the second pass has used it.
Is there performance differences based on function pointers vs. functors? Is it mainly to make people not as confused while reading C++ code? Why would I want a Functor instead of a function pointer for my purposes, or even general purposes in a program?
-=-
"Boy? Girl? It does not matter, all that matters is power, and beauty is power. I am beautiful, so I have power and that is all that matters."
Got Immortality?
Is there performance differences based on function pointers vs. functors? Is it mainly to make people not as confused while reading C++ code? Why would I want a Functor instead of a function pointer for my purposes, or even general purposes in a program?
In a function, persistent state is only possible using static local variables (or an extra ''state'' parameter). There will be ONLY one copy of that state for the whole program.
Two separate calls to the function will always work upon the same persistent state.
A functor is an object. Persistent state may be stored in member variables, both static (single copy of the state) and non-static (one copy per functor instance). You can therefore combine persistent state and non-interference between separate calls.
If your functors are polymorphic (letting you plug several different functors just like you would plug different function pointers), you won''t see a performance benefit (just the extra flexibility with state), since virtual functions use function pointers underneath.
If the functor is statically typed, you can get a performance boost by inlining its member functions - which cannot be done when calling a function through a function pointer, since an inlined function has no address . As functors are typically used with STL algorithms which incorporate loops, the inlining will come handy during loop unrolling.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
In a function, persistent state is only possible using static local variables (or an extra ''state'' parameter). There will be ONLY one copy of that state for the whole program.
Two separate calls to the function will always work upon the same persistent state.
A functor is an object. Persistent state may be stored in member variables, both static (single copy of the state) and non-static (one copy per functor instance). You can therefore combine persistent state and non-interference between separate calls.
If your functors are polymorphic (letting you plug several different functors just like you would plug different function pointers), you won''t see a performance benefit (just the extra flexibility with state), since virtual functions use function pointers underneath.
If the functor is statically typed, you can get a performance boost by inlining its member functions - which cannot be done when calling a function through a function pointer, since an inlined function has no address . As functors are typically used with STL algorithms which incorporate loops, the inlining will come handy during loop unrolling.
struct Functor{ inline int operator()( int ) { /* do something */ );}std::vector vec( 10 );...std::transform( vec.begin(), vec.end(), Functor() );// will create a temporary Functor object, // pass it to std::transform, which will then call// the inlined operator() on each element // of the vector. The compiler may unroll the loop,// because std::transform is a templated function.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
Okay, I got it--I can now think of atleast one place where it would be used
-=-
"Boy? Girl? It does not matter, all that matters is power, and beauty is power. I am beautiful, so I have power and that is all that matters."
Got Immortality?
-=-
"Boy? Girl? It does not matter, all that matters is power, and beauty is power. I am beautiful, so I have power and that is all that matters."
Got Immortality?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement