Hello, I have been trying to write a functor template for a week now
and I'm just having tons of trouble because I don't understand an issue
that I guess is pretty basic to this task.
functor<bool (long, long)> myFunctor;
myFunctor = AFunctionOfThatPrototype;
To get that much is elementary because the template can just store a
(bool)(long,long) as a member variable in functor<T>. Here is the
problem:
bool whether = myFunctor(1,2);
How can I possibly ever utilize the individual result and parameter
type from the prototype in functor::operator()? I have been poring over
several template libraries and I just can't see how they accomplish it
with partial specialization. After all, I can't debug the compiler
itself or look at preprocessor source to figure out what's happening.
I understand that I could always settle for this syntax fairly easily:
functor<bool, long, long> myFunctor;
bool whether = myFunctor(4,5);
And then use a typedef inside the template to make the prototype. But
the libraries I've seen don't have to settle for that. How is this
trick done??? If it matters, I'm using XCode. Thank you very much in
advance,
Yours,
Dan 8 1948
<da*************@gmail.com> wrote... Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task.
functor<bool (long, long)> myFunctor;
Perhaps you wanted
functor<bool(*)(long,long)> myFunctor;
myFunctor = AFunctionOfThatPrototype;
To get that much is elementary because the template can just store a (bool)(long,long) as a member variable in functor<T>. Here is the problem:
bool whether = myFunctor(1,2);
How can I possibly ever utilize the individual result and parameter type from the prototype in functor::operator()?
I am not sure what you mean here, but you need to implement that operator
to "utilize the individual result".
I have been poring over several template libraries and I just can't see how they accomplish it with partial specialization. After all, I can't debug the compiler itself or look at preprocessor source to figure out what's happening.
I understand that I could always settle for this syntax fairly easily:
functor<bool, long, long> myFunctor; bool whether = myFunctor(4,5);
And then use a typedef inside the template to make the prototype. But the libraries I've seen don't have to settle for that. How is this trick done??? If it matters, I'm using XCode. Thank you very much in advance,
Well, if I understood your requirements and your comprehension of the
problem, you should be able to fill in the blanks here:
template<class F> class functor
{
F f; // that's something to what your functor
// delegates the functionlity
public:
functor& operator = (F ff) { ??? }
bool operator ()(long l1, long l2) { ??? }
};
The blanks I was talking about are indicated by ???
Of course, a parameterized constructor might be useful as well, so you
could write
functor<bool (*)(long,long)> myFunctor = AFunctionOfThatPrototype;
instead of using two statements.
Good luck!
V
wrote in news:11*********************@o13g2000cwo.googlegro ups.com in
comp.lang.c++: I understand that I could always settle for this syntax fairly easily:
functor<bool, long, long> myFunctor; bool whether = myFunctor(4,5);
#include <iostream>
#include <ostream>
int function( int a, int b )
{
std::cout << "function( " << a << ", " << b << " );\n";
return 0;
}
/* declaration, no defenition
*/
template < typename F > struct functor;
/* Example, just the one specialization
*/
template < typename R, typename A1, typename A2 >
struct functor< R( A1, A2 ) >
{
functor( R arg(A1, A2) ) : f( arg ) {}
R operator () ( A1 a1, A2 a2 )
{
return f( a1, a2 );
}
private:
R (*f) ( A1, A2 );
};
int main()
{
functor< int( int, int ) > f( function );
return f( 1, 2 );
}
You will need to provide a specialization for each arity (number
of arguments) you want to support, also you might want to use
somthing to add `const &` to the paramiter types of the operator.
Untested code:
....
R operator () (
typename add_cref< A1 >::type a1,
typename add_cref< A2 >::type a2
)
{
return f( a1, a2 )
}
....
template < typename T > struct add_cref
{
typedef T const &type;
};
template < typename T > struct add_cref< T const & >
{
typedef T const &type;
};
template < typename T > struct add_cref< T & >
{
typedef T &type;
};
HTH.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
Thank you Victor and Rob. Peace.
Dan
Hmm. Couple of interesting things...
1. There seems to be no problem using const& like anything else, so I
haven't implemented add_cref.
2. When the prototype has no parameters I have to phrase it different
ways in different compilers.
template < typename R > class functor< R() > // this is fine in xcode,
breaks metrowerks
template < typename R > class functor< R(*)() > // vice versa
Weird...I'll keep exploring.
Dan
wrote in news:11*********************@z14g2000cwz.googlegro ups.com in
comp.lang.c++: Hmm. Couple of interesting things...
1. There seems to be no problem using const& like anything else, so I haven't implemented add_cref.
Quite right too, never solve problems you don't have :). 2. When the prototype has no parameters I have to phrase it different ways in different compilers.
template < typename R > class functor< R() > // this is fine in xcode, breaks metrowerks
It should work, try: < R( void ) >.
Also its a good idea to give compiler version and platform as well
as vendor, somebody with the same or a similar configuration might
be able to provide a work around. Unfortunatly I don't have a
Metroworks compiler so I can't look for one myself.
template < typename R > class functor< R(*)() > // vice versa
Weird...I'll keep exploring.
Not really R(*)() is function-pointer, R() is function type
and this is one of the situations where a function type doesn't
decay to a function-pointer type, IOW it really shouldn't work.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
Well, since it isn't supposed to work I found a different way: if the
function returns a Homer, you just declare functor<Homer>, which is
reasonable to me as long as I can still declare functor<Homer (J,
Simpson)>. Following source is how I do it for 0 params. The emptiness
param is so it doesn't confuse the partial specialization with the
original definition.
(BTW Rob, putting void in the defintion seems to make no difference on
XCode 1.5 or MWCW 8. What compiler are you using?)
struct emptiness {};
template < typename F , typename E=emptiness > class functor;
// zero params alt syntax
template < typename R > class functor< R, emptiness>
{
typedef R(*pt)();
pt _pt;
public:
functor( ) : _pt( 0 ) {}
functor( pt arg ) : _pt( arg ) {}
R operator () ( ) const { return _pt( ); }
functor& operator = (pt arg) { _pt = arg; return *this; }
};
wrote in news:11**********************@l41g2000cwc.googlegr oups.com in
comp.lang.c++: Well, since it isn't supposed to work I found a different way: if the function returns a Homer, you just declare functor<Homer>, which is reasonable to me as long as I can still declare functor<Homer (J, Simpson)>. Following source is how I do it for 0 params. The emptiness param is so it doesn't confuse the partial specialization with the original definition.
(BTW Rob, putting void in the defintion seems to make no difference on XCode 1.5 or MWCW 8. What compiler are you using?)
struct emptiness {}; template < typename F , typename E=emptiness > class functor; // zero params alt syntax template < typename R > class functor< R, emptiness> { typedef R(*pt)(); pt _pt;
public: functor( ) : _pt( 0 ) {} functor( pt arg ) : _pt( arg ) {} R operator () ( ) const { return _pt( ); } functor& operator = (pt arg) { _pt = arg; return *this; } };
Looks good, but I'm not sure I see the need for the emptyness
paramiter why not just have the unspecialized version handle
0 paramiter case:
template < typename R > class functor
{
typedef R(*pt)();
pt _pt;
public:
functor( ) : _pt( 0 ) {}
functor( pt arg ) : _pt( arg ) {}
R operator () ( ) const { return _pt( ); }
functor& operator = (pt arg) { _pt = arg; return *this; }
};
Rob.
-- http://www.victim-prime.dsl.pipex.com/
Hmm...never even thought of that! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: CoolPint |
last post by:
Can anyone explain how I can make the following function accept an
default arguement for the last parameter, which should be an optional
functor?
template <typename T, typename FUNCTOR>
void...
|
by: CoolPint |
last post by:
I am trying to write a generic heapsort (of course as a self-exercise)
with Iterator interface: something like blow....
But I got into trouble finding out the Iterator to the Child node. If...
|
by: CoolPint |
last post by:
I have implemented a generic priority queue below and tested it works
fine, but I have one small problem I cannot understand. I have type
parameter F which determines the priority so that users can...
|
by: daniel.w.gelder |
last post by:
I wrote a template class that takes a function prototype and lets you
store and call a C-level function, like this:
inline string SampleFunction(int, bool) {..}
functor<string (int, bool)>...
|
by: aaragon |
last post by:
Hi everyone,
I'm trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:
template <class Functor>
class ClassA
{...
|
by: Daniel T. |
last post by:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine
// assume dst is zero initialized upon entry
void packBits( u8* dst,...
|
by: Lionel B |
last post by:
I have a function which takes a functor argument. I which to call it for a
functor which is actually a class member; this works fine, using
the mem_fun_ref and bind1st functions (see listing 1...
|
by: alan |
last post by:
Hello all, I'd like to know if there is a nice method of defining a
functor creator which accepts an N-ary function and returns a functor
based on that function.
For example I have a function:...
|
by: aaragon |
last post by:
Hi guys,
Is there a way to return a functor from a recursive call that takes
different paths? Let's say that I have a tree structure like:
root
|
first child ---- nextSibling ----nextSibling...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |