我正在研究一个可能被称为“重载 lambda”的 C++11 习语:
- http://cpptruths.blogspot.com/2014/05/fun-with-lambdas-c14-style-part-2.html
- http://martinecker.com/martincodes/lambda-expression-overloading/
使用可变参数模板重载n 函数似乎对我很有吸引力,但事实证明它不适用于变量捕获:[&]
中的任何一个| [=]
[y]
[&y]
(和 [this]
等,如果在成员函数中)导致编译失败:error: no match for call to '(overload<main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)> >) (char*&)'
(使用我本地的 GCC 4.9.1 和 ideone.com GCC 5.1)
另一方面,固定的 2 进制情况没有遇到这个问题。 (试试把ideone.com上的第一个#if 0
改成#if 1
)
对这里发生的事情有什么想法吗?这是编译器错误,还是我偏离了 C++11/14 规范?
#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]);
}
最佳答案
重载解析仅适用于存在于公共(public)范围内的函数。这意味着第二个实现无法找到第二个重载,因为您没有从 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...);
}
在c++17 , 类模板参数推导和包扩展 using
声明到位,上述实现可以简化为:
template <typename... Ts>
struct overload : Ts... { using Ts::operator()...; };
template <typename... Ts>
overload(Ts...) -> overload<Ts...>;
关于C++11 "overloaded lambda"带有可变参数模板和变量捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33335609/