[英]convert type int(C::*)(int, char) to type int(int, char)
我有一堂课:
struct C {
int F(int, char) { return 0; }
};
并且我需要创建一个std::function
,它将为变量c
调用C::F
函数:
C c;
std::function<int(int, char)> f;
...
f = std::bind(&C::F, &c, _1, _2);
但是如果函数的签名被更改,我也需要更改std :: function。
所以我不想重复签名:
C c;
std::function<delete_class<decltype(&C::F)>::type> f;
...
f = std::bind(&C::F, &c, _1, _2);
其中delete_class是一些魔术助手,它将int(C::*)(int, char)
类型更改为int(int, char)
。
我怀疑可以借助boost::mpl
或boost::function_types
实现它,但是我做不到。
有经验的人可以告诉我该怎么做吗?
PS。 VS 2010
如果您需要一种可以根据需要工作的特征trait delete_class
,则可以完成以下工作:
template<typename S>
struct delete_class;
template<typename R, typename C, typename... Ts>
struct delete_class<R (C::*)(Ts...)>
{
using type = R(Ts...);
};
然后将满足以下声明:
static_assert(
std::is_same<delete_class<decltype(&C::F)>::type,
int(int, char)
>::value, "!");
然后,您可以按照建议的方式使用delete_class<>
:
std::function<delete_class<decltype(&C::F)>::type> f;
C c;
f = std::bind(&C::F, &c, _1, _2);
这是一个生动的例子 。
编辑:
如果仅限于VC10支持(即没有可变参数模板),则必须定义delete_class
主模板的几个部分专业化:
template<typename S>
struct delete_class;
template<typename R, typename C>
struct delete_class<R (C::*)()>
{
typedef R(type)();
};
template<typename R, typename T>
struct delete_class<R (C::*)(T)>
{
typedef R(type)(T);
};
template<typename R, typename T, typename U>
struct delete_class<R (C::*)(T, U)>
{
typedef R(type)(T, U);
};
template<typename R, typename T, typename U, typename V>
struct delete_class<R (C::*)(T, U, V)>
{
typedef R(type)(T, U, V);
};
// And so on...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.