C++ std::tr1::mem\u fn返回类型

C++ std::tr1::mem\u fn返回类型,c++,std,tr1,C++,Std,Tr1,我想把结果放在这里: std::tr1::mem_fn(&ClassA::method); 在变量内部,该变量的类型是什么 看起来是这样的: MagicalType fun = std::tr1::mem_fn(&ClassA::method); 另外,std::tr1::bind的结果类型是什么 谢谢大家! 未指定std::tr1::mem_fn和std::tr1::bind的返回类型 您可以将std::tr1::bind的结果存储在std::tr1::function中:

我想把结果放在这里:

std::tr1::mem_fn(&ClassA::method);
在变量内部,该变量的类型是什么

看起来是这样的:

MagicalType fun = std::tr1::mem_fn(&ClassA::method);
另外,std::tr1::bind的结果类型是什么


谢谢大家!

未指定
std::tr1::mem_fn
std::tr1::bind
的返回类型

您可以将std::tr1::bind的结果存储在
std::tr1::function
中:

struct ClassA { 
    void Func() { }
};

ClassA obj;
std::tr1::function<void()> bound_memfun(std::tr1::bind(&ClassA::Func, obj));
std::tr1::function<void(ClassA&)> memfun_wrap(std::tr1::mem_fn(&ClassA::Func));

未指定
mem_fn
bind
的返回类型。这意味着,根据参数返回不同类型的对象,标准没有规定必须如何实现此功能的细节

如果您想在特定库实现的特定情况下(出于理论上的兴趣,我希望),找出类型是什么,您总是可以导致错误,并从错误消息中获取类型。例如:

#include <functional>

struct X
{
    double method(float);
};

int x = std::mem_fn(&X::method);

9 Untitled.cpp cannot convert 'std::_Mem_fn<double (X::*)(float)>' to 'int' in initialization

该函数可以通过以下方式实现(因此您也可以看到返回类型):您可以在这里尝试这段代码。不幸的是,它使用的变量模板只是C++11标准的一部分

 #include <iostream>
#include <string>

template <class R, class T, class... Args > class MemFunFunctor
{
  private:
  R (T::*mfp)(Args... ); //Pointer to a member function of T which returns something of type R and taking an arbitrary number of arguments of any type 

public:
  explicit MemFunFunctor(R (T::*fp)(Args... ) ):mfp(fp) {}

  R operator()(T* t, Args... parameters)
  {
      (t->*mfp)(parameters... );
  }

};

template <class R,class T, class... Args> MemFunFunctor<R,T,Args... > my_mem_fn( R (T::*fp)(Args... ) )
{
    return MemFunFunctor<R,T,Args... >(fp);   
}


class Foo //Test class
{
    public:
        void someFunction(int i, double d, const std::string& s )
        {
            std::cout << i << " " << d << " " << s << std::endl;
        }
};


int main() //Testing the above code
{

    Foo foo;
    auto f = my_mem_fn(&Foo::someFunction);

    f(&foo, 4, 6.7, "Hello World!" ); //same as foo.someFunction(4, 6.7, "Hello World!");

    return 0;
}
#包括
#包括
模板类MemFunFunctor
{
私人:
R(T::*mfp)(Args…;//指向T的成员函数的指针,该函数返回类型为R的内容,并接受任意数量的任何类型的参数
公众:
显式MemFunFunctor(R(T::*fp)(Args…):mfp(fp){}
R运算符()(T*T,参数…参数)
{
(t->*mfp)(参数…);
}
};
模板MemFunFunctor my_mem_fn(R(T::*fp)(Args…)
{
返回函数functor(fp);
}
类Foo//测试类
{
公众:
void someFunction(int i,双d,常量std::string&s)
{

std::我可以尝试使用
std::tr1::function fun=std::tr1::mem_fn(&ClassA::method);
但它没有编译,有什么不对?@Tarantula:mem_fn
返回的可调用对象采用引用,而不是指针。请查看更新的答案。仍然不起作用,我的Func原型是:virtual void Func(MyType*参数)=0;@Tarantula:如果它是一个接受参数的成员函数,那么生成的可调用对象将接受两个参数:对调用成员函数的对象的引用和作为成员函数参数传递的指针。在这种情况下,它将是一个
函数
。您能提供一些注释/描述吗你的密码在哪?
 #include <iostream>
#include <string>

template <class R, class T, class... Args > class MemFunFunctor
{
  private:
  R (T::*mfp)(Args... ); //Pointer to a member function of T which returns something of type R and taking an arbitrary number of arguments of any type 

public:
  explicit MemFunFunctor(R (T::*fp)(Args... ) ):mfp(fp) {}

  R operator()(T* t, Args... parameters)
  {
      (t->*mfp)(parameters... );
  }

};

template <class R,class T, class... Args> MemFunFunctor<R,T,Args... > my_mem_fn( R (T::*fp)(Args... ) )
{
    return MemFunFunctor<R,T,Args... >(fp);   
}


class Foo //Test class
{
    public:
        void someFunction(int i, double d, const std::string& s )
        {
            std::cout << i << " " << d << " " << s << std::endl;
        }
};


int main() //Testing the above code
{

    Foo foo;
    auto f = my_mem_fn(&Foo::someFunction);

    f(&foo, 4, 6.7, "Hello World!" ); //same as foo.someFunction(4, 6.7, "Hello World!");

    return 0;
}