函数生成从成员函数指针类型 [英] functor generation from member function pointer type

查看:126
本文介绍了函数生成从成员函数指针类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简化(通过 make_fn())生成预处理参数的函数(通过 wrap() 的成员函数。

生成函数基本上是工作,但直到现在只有明确指定成员函数的参数类型。 >
现在我想从它处理的成员函数类型生成正确的函子:

  struct X { }; 

template< class C,typename T1,bool(C :: * F)(T1)>
inline //有更多的T1..TN
bool wrap(C * c,X x)
{
return(c-> * F) T1>(x));
}

template< class C,typename T1,bool(C :: * F)(T1)
inline //对于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);
}

但是,vc ++和g ++看不到 F 作为 make_fn()的参数的类型。



这个想法是应该这样工作:

  struct A 
{
bool f1(bool){return true; }
};

void test()
{
A a;
X x;
make_fn(& A :: f1)(& a,x);有关如何进行这项工作的任何想法吗?





$ b

$ b

背景:

我有一个固定界面,简化后如下:

  bool invoke(C * c,const char * const functionName,int argCount,X * args); 

X是一个变体类型,我必须转换为某些后端类型(int,std :: string ,...)。
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

$ b包装的意图是避免手动转换,而是生成为我或 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→x)(处理< T1>(x));
}
};

template< class C,typename T1>
inline //有更多的T1..TN
boost :: function< bool(C *,X)> make_fn(bool(C :: * f)(T1))
{
return boost :: bind(wrap1< C,T1,bool(C :: *)(T1)> ,_1,_2);
}


struct A
{
bool f1(bool){return true; }
};

void test()
{
A a;
X x;
make_fn(& A :: f1)(& a,x);
}

但是,我不知道这是否是好的,创建其余的包装器。对于后者,你可能只是得到一个支持可变模板的编译器。 :)


I am trying to simplify (via make_fn()) the generation of functors that preprocess parameters (via wrap()) for member functions of arity n.
Generating the functors is basically working, but until now only by explicitly specifying the parameter types for the member function.
Now i'd like to generate the correct functor from the member function type it handles:

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);
}

With this however, vc++ and g++ don't see F as a type for the parameter of make_fn(). I must miss something obvious here and am feeling somewhat blind.

The idea was that it should work like this:

struct A 
{
    bool f1(bool) { return true; }
};

void test()
{
    A a;
    X x;
    make_fn(&A::f1)(&a, x);
}

Any ideas on how to make that work?

Background:
I have a fixed interface which, when simplified, looks like this:

bool invoke(C* c, const char* const functionName, int argCount, X* args);

X is a variant type which i have to convert to certain backend types (int, std::string, ...).
To handle these calls i have a map of functors that are looked up by name and map these calls to member functions of some instance.
The intention of the wrapping is to avoid manual conversions and instead generate functors which do the conversion for me or throw. I have this working with a macro based solution, but that solution requires to specify the types and the parameter count explicitly.
Via function overload resolution i hope to generate the correct converting functor implicitly from the member function signature.

解决方案

It appears to me that you are attempting to turn a pointer passed to a function into a non-type template argument, which I'm afraid is not going to work (see comments to your question).

What you could do, is to store the function pointer in a function object. The following appears to compile:

#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);
}

However, I'm not sure if that is any good and how you would create the rest of the wrappers. For the latter you might just get a compiler that supports variadic templates. :)

这篇关于函数生成从成员函数指针类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆