小编Der*_*rek的帖子

c ++ 0x:在lambda arity上重载

我正在尝试创建一个可以使用带有0,1或2个参数的lambda调用的函数.由于我需要代码在g ++ 4.5和vs2010上工作(它不支持可变参数模板或lambda转换到函数指针),我想出的唯一想法是选择基于arity调用哪个实现.以下是我应该如何看待的非工作猜测.有没有办法修复我的代码或者有更好的方法来执行此操作?

#include <iostream>
#include <functional>
using namespace std;

template <class Func> struct arity;

template <class Func>
struct arity<Func()>{ static const int val = 0; };

template <class Func, class Arg1>
struct arity<Func(Arg1)>{ static const int val = 1; };

template <class Func, class Arg1, class Arg2>
struct arity<Func(Arg1,Arg2)>{ static const int val = 2; };

template<class F>
void bar(F f)
{
    cout << arity<F>::val << endl;
}

int main()
{
    bar([]{cout << "test" << endl;});
}
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

8
推荐指数
1
解决办法
2416
查看次数

标签 统计

c++ ×1

c++11 ×1

lambda ×1