开发者

C++ syntax err, abstracting static and member function into one templated class

sorry for the title, i don't know what suitable title of this topic. I have this test code http://ideone.com/V8h5K which as compile error, and I don't know what's wrong with this code:

my intention is to create an abstraction so that I can create a container of generic function pointer which don't care if the function pointer is a pointer to static function or a member function.

#include <stdio.h>
#include <vector>

template<typename R=void, 
typename A=void,
typename F=R (*)(A)> class Method {     
protected: F method;
public   : Method(F methodPtr):method(methodPtr){ };
           virtual R operator()(A argument) { 
              开发者_开发知识库 return this->method(argument); 
           };
           typedef F FuncType;
};

template<typename A, typename F> class Method<void,A,F> {       
protected: F method;
public   : Method(F methodPtr) :method(methodPtr) { };
           virtual void operator()(A argument) { 
               this->method(argument); 
           };
           typedef F FuncType;
};

template<typename R, typename F> class Method<R,void,F> {       
protected: F method;
public   : Method(F methodPtr) :method(methodPtr) { };
           virtual R operator()() { 
               return this->method(); 
           };
           typedef F FuncType;
};

template<typename F> class Method<void,void,F> {        
protected: F method;
public   : Method(F methodPtr) :method(methodPtr) { };
           virtual void operator()() { 
               this->method(); 
           };
           typedef F FuncType;
};

template<typename C=void,
typename R=void, 
typename A=void,
typename F=R (C::*)(A)> 
class ClassMethod : public Method<R,A,F> {      
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
               :Method<R,A,F>(methodPtr),owner(methodOwner){ };
           virtual R operator()(A argument) { 
               return ((this->owner).*(this->method))(argument); 
           };
           typedef F FuncType;
};

template<typename C, typename A, typename F> 
class ClassMethod<C,void,A,F>: public Method<void,A,F> {
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
               :Method<void,A,F>(methodPtr),owner(methodOwner){ };
           virtual void operator()(A argument) { 
               ((this->owner).*(this->method))(argument); 
           };
           typedef F FuncType;
};

template<typename C, typename R, typename F> 
class ClassMethod<C,R,void,F>: public Method<R,void,F> {
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
               :Method<R,void,F>(methodPtr),owner(methodOwner){ };
           virtual R operator()() { 
               return ((this->owner).*(this->method))(); 
           };
           typedef F FuncType;
};

template<typename C, typename F> 
class ClassMethod<C,void,void,F>: public Method<void,void,F> {
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
               :Method<void,void,F>(methodPtr),owner(methodOwner){ };
           virtual void operator()() { 
               ((this->owner).*(this->method))(); 
           };
           typedef F FuncType;
};

// ---- implementation -----

template<typename A> class MethodList {
protected:
    std::vector< Method<void,A> > methods;
public: 
    void add(typename Method<void,A>::FuncType fp) {
        this->methods.push_back(Method<void,A>(fp));
    }
    template<class C> void add(typename C& instance,
    typename ClassMethod<C,void,A>::FuncType fp) {
        this->methods.push_back(ClassMethod<C,void,A>(instance,fp));
    }
    void invoke(A argument) {
        typename std::vector< Method<void,A> >::iterator it;
        for(it=this->methods.begin() ; it!=this->methods.end() ; it++) {
            (*it)(argument);
        }
    }
};


void function1(int arg) {
    printf("function1(%d)",arg);
}

class Class1 {
public:
    void function1(int arg) {
        printf("Class1::function1(%d)",arg);
    }
};

int main(int argc,char* argv[] )                        
{      
    Class1 inst;
    MethodList<int> methodList;

    methodList.add(function1);
    methodList.add<Class1>(inst,&Class1::function1);
    methodList.invoke(123);

    return 0;
}   


This virtual member function will be instantiated whenever the enclosing class is instantiated on most implementations. You don't have to use it for that instantiation to occur:

virtual R operator()(A argument) { 
    return this->method(argument); 
};

However when Fis a R(C::*)(Args), the use of this->method will be ill-formed. So this derivation is wrong:

// will instantiate Method<R, A, F>
class ClassMethod : public Method<R,A,F>

FWIW you don't need to specialize for R being void. You can have a return statement in a void function with an expression that has type void just fine.

// works fine with R being void
virtual R operator()(A argument) { 
    return static_cast<R>(this->method(argument));
};

WOW, now I see you are also slicing objects in your MethodList class template. If you don't know what "slicing" is about, I recommend you to read on basic C++ topics first, before writing this sort of code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜