我正在研究一种可能被称为"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 …Run Code Online (Sandbox Code Playgroud) c++ lambda template-meta-programming variadic-templates c++11
C++ 14引入了通用lambdas(在lambda签名中使用auto关键字时).
有没有办法将它们存储在带有C++ 17的向量中?
我知道这个现有的问题,但它不适合我的需要:我可以有一个模板函数指针的std :: vector吗?
这是一个示例代码,说明了我想要做的事情.(请在回答前查看底部的注释)
#include <functional>
#include <vector>
struct A {
void doSomething() {
printf("A::doSomething()\n");
}
void doSomethingElse() {
printf("A::doSomethingElse()\n");
}
};
struct B {
void doSomething() {
printf("B::doSomething()\n");
}
void doSomethingElse() {
printf("B::doSomethingElse()\n");
}
};
struct TestRunner {
static void run(auto &actions) {
A a;
for (auto &action : actions) action(a);
B b;
for (auto &action : actions) action(b); // I would like to do it
// C c; ...
}
};
void testCase1() …Run Code Online (Sandbox Code Playgroud)