首页 新闻 搜索 专区 学院

chrome 仿函数模板 总是编译错误,请问哪里出错了?

0
悬赏园豆:50 [待解决问题]

最近我在看chrome 的callback源码, 就照着写了如下一个类模板, 这个类模板接受 函数指针为模板参数,但是怎么也比编译不过,以下是源码, 请问院子里的高人们,我哪里写错了

 

// callback.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

template <typename R, typename A1, typename A2> struct T_Funct;
template <typename R, typename A1, typename A2> struct T_Funct<R (__stdcall *)(A1,A2), A1, A2>{
typedef R (Run)(A1,A2);
R (__stdcall *function_)(A1, A2);
explicit T_Funct(R(__stdcall *function)(A1,A2), A1 a1, A2 a2):function_(function),a1_(a1),a2_(a2){}
R operator()(){
return function_();
}
};

int show_int(int , int ){
return printf("print_int is invoked...\n");
}


int _tmain(int argc, _TCHAR* argv[])
{
typedef int (*p_fun_int)(int, int );
p_fun_int fun_int;
fun_int = show_int;
T_Funct<int , int , int > t_funct(fun_int, 1,2);

return 0;
}

 

 

编译错误如下: 

 

1>------ Build started: Project: callback, Configuration: Debug Win32 ------
1>Compiling...
1>callback.cpp
1>e:\code\template\callback\callback\callback.cpp(26) : error C2079: 't_funct' uses undefined struct 'T_Funct<R,A1,A2>'
1> with
1> [
1> R=int,
1> A1=int,
1> A2=int
1> ]
1>e:\code\template\callback\callback\callback.cpp(26) : error C2078: too many initializers
1>Build log was saved at "file://e:\code\template\callback\callback\Debug\BuildLog.htm"
1>callback - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

还在想啊的主页 还在想啊 | 初学一级 | 园豆:160
提问于:2014-11-14 11:26
< >
分享
所有回答(1)
0

在高人的指教下:

我完成了对 给模板参数传普通函数指针, 给模板参数 传成员函数指针的问题:

#include "stdafx.h"
struct true_type{};
template <typename R> struct T_Funct;
template <typename R, typename A1, typename A2> struct T_Funct<R (*)(A1,A2)>{
    typedef R (Runable)(A1,A2);
    Runable* function_;
    A1 a1_;
    A2 a2_;
    explicit T_Funct(Runable function, A1 a1, A2 a2):function_(function),a1_(a1),a2_(a2){}
    R Run(){
        return function_(a1_,a2_);
    }
    R operator()(){
        return function_(a1_,a2_);
    }

};

template <typename R, typename T, typename A1, typename A2>struct T_Funct<R (T::*)(A1, A2) >{
    typedef true_type IsMethod;

    R (T::*function_)(A1,A2);

    explicit T_Funct(R (T::*method)( A1, A2)):function_(method){}

    R Run(T* object, A1 a1, A2 a2){
        return (object->*function_)(a1,a2);
    }
};

template <typename R, typename T, typename A1, typename A2>struct T_Funct<R (T::*)(A1, A2)const >{
    typedef true_type IsMethod;

    R (T::*function_)(A1,A2)const;

    explicit T_Funct(R (T::*method)( A1, A2)const):function_(method){}

    R Run(T* object, A1 a1, A2 a2){
        return (object->*function_)(a1,a2);
    }
};

template <typename R, typename T, typename A1, typename A2>struct T_Funct<const R (T::*)(A1, A2) >{
    typedef true_type IsMethod;

    const R (T::*function_)(A1,A2);

    explicit T_Funct(const R (T::*method)( A1, A2)):function_(method){}

    R Run(T* object, A1 a1, A2 a2){
        return (object->*function_)(a1,a2);
    }
};

template <typename R, typename T, typename A1, typename A2>struct T_Funct<const R (T::*)(A1, A2)const >{
    typedef true_type IsMethod;

    const R (T::*function_)(A1,A2)const;

    explicit T_Funct(const R (T::*method)( A1, A2)const):function_(method){}

    R Run(T* object, A1 a1, A2 a2){
        return (object->*function_)(a1,a2);
    }
};
int show_int(int , int ){
    return printf("print_int is invoked...\n");
}

struct MyFoo{
    typedef int (MyFoo::*PFUN_NONE)(int , int );
    typedef int (MyFoo::*PFUN_NEXT_CONST)(int , int )const;
    typedef const int (MyFoo::*PFUN_PRIOR_CONST)(int , int );
    typedef const int (MyFoo::*PFUN_NEXT_PRIOR_CONST)(int , int )const;

    int show_myfoo_none(int i, int j){
        return printf("MyFoo::show_myfoo_none is invoked...\n");
    }
    int show_myfoo_next_const(int i, int j)const{
        return printf("MyFoo::show_myfoo_next_const is invoked...\n");
    }
    const int show_myfoo_prior_const(int i, int j){
        return printf("MyFoo::show_myfoo_prior_const is invoked...\n");
    }
    const int show_myfoo_next_prior_const(int i, int j)const{
        return printf("MyFoo::show_myfoo_next_prior_const is invoked...\n");
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    typedef int (*p_fun_int)(int, int );
    p_fun_int fun_int;
    fun_int = show_int;
    
    T_Funct<int (*)( int , int) > t_funct(fun_int, 1,2);
    t_funct();
    MyFoo f;
    
    MyFoo::PFUN_NONE pfun_none;
    MyFoo::PFUN_NEXT_CONST pfun_next_const;
    MyFoo::PFUN_PRIOR_CONST pfun_prior_const;
    MyFoo::PFUN_NEXT_PRIOR_CONST pfun_next_prior_const;

    pfun_none = &MyFoo::show_myfoo_none;
    pfun_next_const = &MyFoo::show_myfoo_next_const;
    pfun_prior_const = &MyFoo::show_myfoo_prior_const;
    pfun_next_prior_const = &MyFoo::show_myfoo_next_prior_const;


    T_Funct<int (MyFoo::*)(int, int )> t_2(pfun_none);
    T_Funct<int (MyFoo::*)(int, int )const> t_3(pfun_next_const);
    T_Funct<const int (MyFoo::*)(int, int )> t_4(pfun_prior_const);
    T_Funct<const int (MyFoo::*)(int, int )const> t_5(pfun_next_prior_const);

    t_2.Run(&f, 2,3);
    t_3.Run(&f, 2,3);
    t_4.Run(&f, 2,3);
    t_5.Run(&f, 2,3);
    return 0;
}
View Code
还在想啊 | 园豆:160 (初学一级) | 2014-11-14 16:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册