Archived

This topic is now archived and is closed to further replies.

Matt Calabrese

Advanced Functor

Recommended Posts

Matt Calabrese    122
Today I had a great idea for templated functor class, but I''m having trouble implementing it, and I fear there may not be a way to do what I''m thinking in C++. It''s a similiar idea as to how "regular" functors work, but what I''m trying to do is be able to get all of my functors to be able to be created from a single templated class definition. Here''s the concept. There are two template parameters -- the first is a datatype and the second is an instance of that type. The only method is a templated overloaded operator () that takes a parameter of a reference to some object. (it''s hard to explain in words because it''s so abstract. I''ll include the code later on). The method takes the class instance and attempts to call the dereferenced second template parameter of the class (so basically it tries to call a method on the object). The idea is that with this single templated class definition, a user can call any method without having to manually create a functor for that particular method. It works great! With the single definition I can make a functor for any class with any method and call it succesfully without ever having to make another functor class definition! ... But there is one problem -- the parameters. With other implementations of functors that I''ve seen, parameters are stored as members of the class. The thing is that since different methods take different parameters, I can''t think of a way that I would be able to account for the parameters for any method call. I realize this could be done by doing what i said earlier -- making completely separate class definitions for different methods. But this is what I''m trying to avoid. If possible I would like to use that single templated class for any functor that I''d ever need. I''m pretty close to that point, but right now it will only work for all classes that have the same types of parameters in the same order. Here''s the class:
  
template< class Methodtype, Methodtype Method >
     class Functor
{
public:
     template< class Classtype >
          operator ()( Classtype& Target )
     {
          (Target.*Method)();
     }
};
  
And the way you''d use it would be something like:
  
Functor< void (MyClass::*)() const, &MyClass::MyMethod > MyFunctor();

MyFunctor( MyObject );
  
The problem is that in the templated functor definition I have to call the method with some parameters and I also need to store those parameters within the functor. Right now it works completely fine with ANY class using ANY method with ANY return type as long as the method takes no parameters! I''m pretty close to my goal, but I don''t think it''s possible to go any further without making separate classes for methods with certain parameters. Any suggestions? And don''t say "make more functor classes" becuase I know that''s a solution but that''s exactly what I''m trying to avoid. Thanks!

Share this post


Link to post
Share on other sites
Fruny    1658
The only template parameters allowed are ''type'' parameters, and integral types. You can''t pass a ''Methodtype Method'' parameter (at least, you shouldn''t ... mumble, mumble... )

On the other hand, that functionality is exactly what boost::bind, boost::mem_fn and boost::function implement ( along with boost::signal and boost::lambda for related concepts ).

Look them up

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Cedric    158
quote:
Original post by Fruny
The only template parameters allowed are ''type'' parameters, and integral types. You can''t pass a ''Methodtype Method'' parameter (at least, you shouldn''t ... mumble, mumble... )

What do you mean, ''mumble, mumble''? Is it Standard or not? I think that Comrade Fruny has lost his faith....

Cédric

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by cedricl
What do you mean, ''mumble, mumble''? Is it Standard or not? I think that Comrade Fruny has lost his faith....


The standard says, integral types. Pointers are not technically integral types, member function pointers even less. Thus, they shouldn''t be allowed in templates. However, given the level of compliance of compilers, I''m not surprised Matt could get it through.

There is talk that the C++ committee will allow, as an exception, const char* const strings as template parameters.

My faith in the Holy Standard shall be strong.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Cedric    158
I don''t have the Standard, but I found someone quoting it:

"From the standard, 14.1.4:
A non-type template-parameter shall have one of the following (optionally cv-qualified) types:

- integral or enumeration type,

- pointer to object or pointer to function,

- reference to object or reference to function,

- pointer to member."

Cédric

Share this post


Link to post
Share on other sites
Fruny    1658
*checks his copy* Indeed, I am wrong, I abjure my crimes.

You can have function pointers, just not pointers to basic types.

I will now retire into solitude and live a life of meditation on my mistakes.

Dang, I was persuaded it was integral types only...

That'll teach me to speak without checking first. I deserve a good flogging.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 20, 2002 8:54:08 PM]

Share this post


Link to post
Share on other sites