关于函数指针与类作模板参数的类型推导有关问题

关于函数指针与类作模板参数的类型推导问题
本帖最后由 shun01 于 2013-08-10 10:53:44 编辑
今天仿STL functional写了一个bind2nd函数,由于functional里的bind2nd只针对函数对象有效,我在编写时增加了对函数指针也可绑定的情况。但是模板参数类型推导时出现了问题,


//funciotn.h
namespace LouisLib
{
        template<class R , class A1 , class A2 >
struct binary_function
{
typedef R result_type ;
typedef A1 argument1_type ;
typedef A2 argument2_type ;
};

        template<class Pred>
struct binary_traits
{//二元函数对象参数和返回值类型

typedef Pred function_type ;
typedef const Pred & param_type ;
typedef typename Pred::result_type result_type ;
typedef typename Pred::argument1_type argument1_type ;
typedef typename Pred::argument2_type argument2_type ;
};


template<class R , class A1 , class A2>
struct binary_traits< R (*)(A1 , A2) >
{//二元指针函数的返回值以及参数类型
typedef R (*function_type)(A1 , A2 ) ;
typedef R (*param_type)(A1 , A2 ) ;
typedef R result_type ;
typedef A1 argument1_type ;
typedef A2 argument2_type ;
};

        template<class Pred>
struct binder2nd:public unary_function<typename binary_traits<Pred>::result_type ,
typename binary_traits<Pred>::argument1_type>
{
private:
typedef typename binary_traits<Pred>::argument2_type arg2_type ;
arg2_type m_arg ;
typename binary_traits<Pred>::function_type m_op ;

public:
binder2nd( typename binary_traits<Pred>::param_type _pred , arg2_type _val)
:m_op( _pred ) , m_arg( _val ){}

result_type operator ()( argument_type _val )const 
{
return m_op( _val , m_arg ) ;
}

result_type operator()( argument_type _val )
{
return m_op( _val , m_arg ) ;
}
};
        
         template<class Pred , class T >
binder2nd<Pred> bind2nd( const Pred & _pred, const T & _bindVal )//若将此处函数参数改为Pred _pred则可以正常操作