c++ - C++0x lambda, how can I pass as a parameter? -
please @ following c++0x lambda related code:
typedef uint64_t (*weight_func)(void* param); typedef std::map<std::string, weight_func> callbacktable; callbacktable table; table["rand_weight"] = [](void* param) -> uint64_t { return (rand() % 100 + 1); };
i got error (in visual studio 2010) lambda couldn't converted type of weight_func
. know answer: using std::function object
:
typedef std::function<uint64_t (void*)> weight_func;
however, want know how can receive type of lambda without using std::function
. type should be?
the conversion function pointer relatively new: introduced n3043 on february 15, 2010.
while e.g. gcc 4.5 implements it, visual studio 10 released on april 12, 2010 , didn't implement in time. james pointed out, will fixed in future releases.
for moment have use 1 of alternative solutions provided here.
technically following workaround work, without variadic templates no fun generalize (boost.pp rescue...) , there no safety net against passing capturing lambdas in:
typedef uint64_t (*weightfunc)(void* param); template<class func> weightfunc make_function_pointer(func& f) { return lambda_wrapper<func>::get_function_pointer(f); } template<class f> class lambda_wrapper { static f* func_; static uint64_t func(void* p) { return (*func_)(p); } friend weightfunc make_function_pointer<>(f& f); static weightfunc get_function_pointer(f& f) { if (!func_) func_ = new f(f); return func; } }; template<class f> f* lambda_wrapper<f>::func_ = 0; // ... weightfunc fp = make_function_pointer([](void* param) -> uint64_t { return 0; });
Comments
Post a Comment