nod*_*kai 9 c++ lambda template-meta-programming variadic-templates c++11
我正在研究一种可能被称为"overloaded lambda"的C++ 11习语:
使用可变参数模板重载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-ary情况没有遇到这个问题.(尝试改变第一#if 0到#if 1上ideone.com)
关于这里发生了什么的任何想法?这是编译器错误,还是我偏离了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]);
}
Run Code Online (Sandbox Code Playgroud)
重载解析仅适用于公共范围中存在的函数.这意味着第二个实施未能找到第二个重载,因为你不从导入函数调用运营商overload<Frest...>进入overload<F0, Frest...>.
但是,非捕获lambda类型将转换运算符定义为具有与lambda函数调用运算符相同的签名的函数指针.可以通过名称查找找到此转换运算符,这是在删除捕获部分时调用的内容.
正确的实现,适用于捕获和非捕获lambdas,并始终调用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...);
}
Run Code Online (Sandbox Code Playgroud)
在c ++ 17中,通过类模板参数推导和using声明包的扩展,上面的实现可以简化为:
template <typename... Ts>
struct overload : Ts... { using Ts::operator()...; };
template <typename... Ts>
overload(Ts...) -> overload<Ts...>;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1660 次 |
| 最近记录: |