c++ 11 lambda函数中的异常规范

Exception specification in C++11 lambda Functions

本文关键字:异常 lambda 函数 c++      更新时间:2023-10-16

对于命名函数,可以指定可能抛出的异常,如

所示
void func(void) throw (string);

如何在c++ lambda函数中指定异常?

也一样。[expr.prim]中lambda的相关语法。λ):

lambda表达式:
lambda-declaratoropt复合语句

lambda-introducer :
,,,,[ lambda-captureopt ]

lambda-declarator :
,,,,( 参数声明子句 ) mutable opt
               exception-specificationoptattribute- specific -seqopt尾随返回类型optopt

可以在参数之后提供可选的异常规范。例如:

auto never_throws = []() noexcept {
    return 5;
};
int i = never_throws(); // won't throw