考虑这段代码:
auto lambda=[] () { std::cout << "Error occurred!" << std::endl; };
auto lambda_2=[=/*,lambda*/] () {
try {
// Do something
std::cout << (2+2) << std::endl;
} catch (...) {
lambda();
throw;
}
};
我的期望:这段代码可以编译。
现实(MinGW 4.8.0):
main.cpp: In lambda function:
main.cpp:27:5: error: '...' handler must be the last handler for its try block [
-fpermissive]
} catch (...) {
^
如果我取消注释显式的按副本捕获lambda
,则代码会编译,但这会发出警告:
main.cpp:20:19: warning: explicit by-copy capture of 'lambda' redundant with by-
copy capture default [enabled by default]
auto lambda_2=[=,lambda] () {
^
这是 GCC 的问题,还是我不理解 lambda 捕获的某些方面?
编辑:以下编译:
auto lambda=[] () { std::cout << "Error occurred!" << std::endl; };
auto lambda_2=[&] () {
try {
// Do something
std::cout << (2+2) << std::endl;
} catch (...) {
try {
lambda();
} catch (...) { }
throw;
}
};