Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模拟std::函数模板参数_C++_Templates_Std_Std Function - Fatal编程技术网

C++ 模拟std::函数模板参数

C++ 模拟std::函数模板参数,c++,templates,std,std-function,C++,Templates,Std,Std Function,我想模拟std::函数模板参数,但我不知道它到底是如何工作的。 考虑这个代码,例如: std::function<int(int)> p; 我希望MemFuncPointerType为以下类型: int (__thiscall MyClass::* )(int) 以及: 我使用的是VS2010,因此并非所有的C++11功能都受支持,但它确实实现了std::function。使用可变模板: template <typename C, typename T> struct

我想模拟std::函数模板参数,但我不知道它到底是如何工作的。 考虑这个代码,例如:

std::function<int(int)> p;
我希望MemFuncPointerType为以下类型:

int (__thiscall MyClass::* )(int)
以及:


我使用的是VS2010,因此并非所有的C++11功能都受支持,但它确实实现了std::function。

使用可变模板:

template <typename C, typename T>
struct make_member_function_pointer;

template <typename C, typename R, typename... Args>
struct make_member_function_pointer<C,R(Args...)>
{
    using type = R(C::*)(Args...);
};
template <typename T> struct identity { typedef T type; };

template <typename C, typename T>
struct make_member_function_pointer;
template <typename C, typename R, typename Arg1>
struct make_member_function_pointer<C,R(Arg1)> : identity<R(C::*)(Arg1)> {};
template <typename C, typename R, typename Arg1, typename Arg2>
struct make_member_function_pointer<C,R(Arg1,Arg2)> : identity<R(C::*)(Arg1,Arg2)> {};
template <typename C, typename R, typename Arg1, typename Arg2, typename Arg3>
struct make_member_function_pointer<C,R(Arg1,Arg2,Arg3)> : identity<R(C::*)(Arg1,Arg2,Arg3)> {};
没有可变模板:

template <typename C, typename T>
struct make_member_function_pointer;

template <typename C, typename R, typename... Args>
struct make_member_function_pointer<C,R(Args...)>
{
    using type = R(C::*)(Args...);
};
template <typename T> struct identity { typedef T type; };

template <typename C, typename T>
struct make_member_function_pointer;
template <typename C, typename R, typename Arg1>
struct make_member_function_pointer<C,R(Arg1)> : identity<R(C::*)(Arg1)> {};
template <typename C, typename R, typename Arg1, typename Arg2>
struct make_member_function_pointer<C,R(Arg1,Arg2)> : identity<R(C::*)(Arg1,Arg2)> {};
template <typename C, typename R, typename Arg1, typename Arg2, typename Arg3>
struct make_member_function_pointer<C,R(Arg1,Arg2,Arg3)> : identity<R(C::*)(Arg1,Arg2,Arg3)> {};
用法:

测试:

更新

我是否可以利用预处理器自动生成make_member_函数_指针的部分专门化?我见过类似的东西是使用BOOST_PP_迭代完成的,但我不知道它是如何工作的

当然可以:

测试:

intint只是一种类型,因此可以通过模板进行匹配

肯定太笼统了:

template<typename T>
struct Pointer
{
    typedef T* type;
};
然后你就可以做了

int foo(int i) {return i;}

int main() {
    Pointer<int(int)>::type f = &foo;
}

VS2010不支持可变模板,我的目标是:模板结构MyStruct{typedef doSomethingWihtT funcPointer;}。我想以某种方式从模板参数捕获并定义函数指针,而不重载变量参数number。@AlejandroFreeman您想让它只与函数指针一起工作吗?我希望它是什么样的泛型,不仅仅是int*funcint函数的意思?很抱歉我没有解释清楚。我正在尝试实现类似于SomeSmartStruct::MemFuncPointerType pMemFunc;我希望MemFuncPointerType为以下类型int u thiscall MyClass::*int.SomeSmartStruct::FunctionPointer pFunc;pFunc应该是int u cdecl*int类型。可以这样做吗?非常感谢!如果我想要3个以上的参数,我需要创建另一个make_member_function_pointer结构来处理它?@AlejandroFreeman是的,只需添加一些更精确的部分专门化函数类型。
template <typename T, typename F>
struct SomeSmartStruct
{
    typedef typename make_member_function_pointer<T,F>::type MemFuncPointerType;
    typedef F* FunctionPointer;
};
struct MyClass
{
    int foo(int) {return 0;}
};
int bar(int) {return 0;}

int main()
{
    SomeSmartStruct<MyClass, int(int)>::MemFuncPointerType pMemFunc = &MyClass::foo;
    SomeSmartStruct<MyClass, int(int)>::FunctionPointer pFunc = &bar;
}
#include <boost/preprocessor.hpp>

template <typename T> struct identity { typedef T type; };

template <typename C, typename T>
struct make_member_function_pointer;

#define BOOST_PP_LOCAL_MACRO(n)\
    template <typename C, typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Arg)>\
    struct make_member_function_pointer<C,R(BOOST_PP_ENUM_PARAMS(n, Arg))> : identity<R(C::*)(BOOST_PP_ENUM_PARAMS(n, Arg))> {};
#define BOOST_PP_LOCAL_LIMITS (0, 20) // 20 is the limit of params
#include BOOST_PP_LOCAL_ITERATE()
int bar10(int,int,int,int,int,int,int,int,int,int) {return 0;}

SomeSmartStruct<MyClass, int(int,int,int,int,int,int,int,int,int,int)>::FunctionPointer pFunc10 = &bar10;
template<typename T>
struct Pointer
{
    typedef T* type;
};
int foo(int i) {return i;}

int main() {
    Pointer<int(int)>::type f = &foo;
}