C++11 "overloaded lambda" 带有可变参数模板和变量捕获

C++11 "overloaded lambda" with variadic template and variable capture

我正在研究可能称为 "overloaded lambda":

的 C++11 习语

使用可变参数模板重载 n 函数似乎对我很有吸引力,但事实证明它不适用于变量捕获:任何 [&] [=] [y] [&y] (和 [this] etc if in a member function) 导致编译失败: error: no match for call to '(overload<main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)> >) (char*&)' (with my local GCC 4.9.1 and ideone.com GCC 5.1)

另一方面,固定的 2 元情况下没有遇到这个问题。 (尝试将第一个 #if 0 更改为 ideone.com 上的 #if 1

对这里发生的事情有什么想法吗?这是编译器错误,还是我偏离了 C++11/14 规范?

http://ideone.com/dnPqBF

#include <iostream>
using namespace std;

#if 0
template <class F1, class F2>
struct overload : F1, F2 {
  overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }

  using F1::operator();
  using F2::operator();
};

template <class F1, class F2>
auto make_overload(F1 f1, F2 f2) {
  return overload<F1, F2>(f1, f2);
}
#else
template <class... Fs>
struct overload;

template <class F0, class... Frest>
struct overload<F0, Frest...> : F0, overload<Frest...> {
  overload(F0 f0, Frest... rest) : F0(f0), overload<Frest...>(rest...) {}

  using F0::operator();
};

template <>
struct overload<> {
  overload() {}
};

template <class... Fs>
auto make_overload(Fs... fs) {
  return overload<Fs...>(fs...);
}
#endif

#if 0
#define CAP
#define PRINTY()
#else
#define CAP y
#define PRINTY() cout << "int y==" << y << endl
#endif

int main(int argc, char *argv[]) {
    int y = 123;

    auto f = make_overload(
        [CAP] (int x) { cout << "int x==" << x << endl; PRINTY(); },
        [CAP] (char *cp) { cout << "char *cp==" << cp << endl; PRINTY(); });
    f(argc);
    f(argv[0]);
}

重载解析仅适用于存在于公共范围内的函数。这意味着第二个实现无法找到第二个重载,因为您没有将函数调用运算符从 overload<Frest...> 导入到 overload<F0, Frest...>.

但是,非捕获 lambda 类型将转换运算符定义为函数指针,其签名与 lambda 的函数调用运算符相同。这个转换运算符可以通过名称查找找到,这就是当你删除捕获部分时调用的。

适用于捕获和非捕获 lambda 且始终调用 operator() 而不是转换运算符的正确实现应如下所示:

template <class... Fs>
struct overload;

template <class F0, class... Frest>
struct overload<F0, Frest...> : F0, overload<Frest...>
{
    overload(F0 f0, Frest... rest) : F0(f0), overload<Frest...>(rest...) {}

    using F0::operator();
    using overload<Frest...>::operator();
};

template <class F0>
struct overload<F0> : F0
{
    overload(F0 f0) : F0(f0) {}

    using F0::operator();
};

template <class... Fs>
auto make_overload(Fs... fs)
{
    return overload<Fs...>(fs...);
}

DEMO

中,使用 class 模板参数推导和 using 声明的包扩展,上述实现可以简化为:

template <typename... Ts> 
struct overload : Ts... { using Ts::operator()...; };

template <typename... Ts>
overload(Ts...) -> overload<Ts...>;

DEMO 2

C++11 中重载的平面版本

回复已接受答案的评论者,这是一个根本不使用递归模板的版本。这允许您根据需要进行尽可能多的重载,并且只调用一侧模板。

  namespace details {
    template<class F>
    struct ext_fncall : private F {
      ext_fncall(F v) :
        F(v) {}
      
      using F::operator();
    };
  }
  
  template<class... Fs>
  struct overload : public details::ext_fncall<Fs>... {
    overload(Fs... vs) :
      details::ext_fncall<Fs>(vs)... {}
  };
  
  template<class... Fs>
  overload<Fs...> make_overload(Fs... vs) {
    return overload<Fs...> {vs...};
  }

说明

副模板 ext_fncall<class F> 派生自给定的仿函数,仅公开其 operator(),它模仿给定的 C++11 版本。

实际的overload<class... Fs>派生自ext_fncall<Fs>...,也就是说它只暴露了它派生的类中的operator()(其他成员无法访问,因为ext_fncall<F>).