Geo*_*che 5 c++ templates functor
我试图简化(通过make_fn())wrap()为arity n的成员函数预处理参数(via )的仿函数的生成.
生成仿函数基本上是可行的,但直到现在只能通过显式指定成员函数的参数类型.
现在我想从它处理的成员函数类型生成正确的函子:
struct X {};
template<class C, typename T1, bool (C::*F)(T1)>
inline // there are more for T1..TN
bool wrap(C* c, X x)
{
return (c->*F)(process<T1>(x));
}
template<class C, typename T1, bool (C::*F)(T1)>
inline // there are more for T1..TN
boost::function<bool (C*, X)> make_fn(F f) // <- problem here, F is not a type
{
return boost::bind(&wrap<C, T1, F>, _1, _2);
}
Run Code Online (Sandbox Code Playgroud)
但是,有了这个,vc ++和g ++看不到F参数的类型make_fn().我必须在这里错过一些明显的东西,感觉有些失明.
这个想法应该是这样的:
struct A
{
bool f1(bool) { return true; }
};
void test()
{
A a;
X x;
make_fn(&A::f1)(&a, x);
}
Run Code Online (Sandbox Code Playgroud)
关于如何使这项工作的任何想法?
背景:
我有一个固定的界面,简化后,如下所示:
bool invoke(C* c, const char* const functionName, int argCount, X* args);
Run Code Online (Sandbox Code Playgroud)
X是一种变体类型,我必须转换为某些后端类型(int,std :: string,...).
为了处理这些调用,我有一个按名称查找的仿函数映射,并将这些调用映射到某个实例的成员函数.
包装的目的是避免手动转换,而是生成为我或者转换的仿函数throw.我使用基于宏的解决方案,但该解决方案需要明确指定类型和参数计数.
通过函数重载决策,我希望从成员函数签名中隐式生成正确的转换函子.
在我看来,您正在尝试将传递给函数的指针转换为非类型模板参数,这恐怕行不通(请参阅对您问题的评论)。
您可以做的是将函数指针存储在函数对象中。编译结果如下:
#include <boost/bind.hpp>
#include <boost/function.hpp>
struct X {};
template <class T>
bool process(X) { return true; }
template <class C, class T1, class Func>
struct wrap1
{
typedef bool result_type;
Func f;
wrap1(Func f): f(f) {}
bool operator()(C* c, X x)
{
return (c->*f)(process<T1>(x));
}
};
template<class C, typename T1>
inline // there are more for T1..TN
boost::function<bool (C*, X)> make_fn(bool (C::*f)(T1))
{
return boost::bind(wrap1<C, T1, bool (C::*)(T1)>(f), _1, _2);
}
struct A
{
bool f1(bool) { return true; }
};
void test()
{
A a;
X x;
make_fn(&A::f1)(&a, x);
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定这是否有什么好处以及如何创建其余的包装器。对于后者,您可能只需要一个支持可变参数模板的编译器。:)
| 归档时间: |
|
| 查看次数: |
2018 次 |
| 最近记录: |