c++ - 如何向 lambda 函数添​​加属性?

标签 c++ lambda attributes noreturn

<分区>

假设我有一个 lambda

auto func = [](std::string msg) { throw std::runtime_error(msg); };

(不可否认,这个例子没有什么意义,但这不是重点)。如果这不是 lambda,而是普通函数,我会用 noreturn 属性声明它,如

[[noreturn]] void func(std::string msg) { throw std::runtime_error(msg); }

这也可以用于 lambda 吗? (我尝试了 clang 3.5 的几种变体,但没有成功。)


编辑 使用 Apple LLVM 版本 6.0 (clang-600.0.57)(基于 LLVM 3.5svn),我试过了

auto func = [](std::string msg) -> [[noreturn]] void { throw std::runtime_error(msg); };

auto func = [](std::string msg) [[noreturn]] { throw std::runtime_error(msg); };

但都被拒绝了。这是 clang 3.5 的不完整/错误吗?

最佳答案

以下适用于 g++ 4.9.3:

void foo()
{
  auto func = [] (std::string msg) [[noreturn]] { throw std::runtime_error(msg); };
}

http://ideone.com/49Ietf 工作也是。

关于c++ - 如何向 lambda 函数添​​加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32531626/

相关文章:

c++ - 我可以在运行时更改 Log4Qt 配置的文件名吗?

c++ - 不为派生类型调用函数模板特化

C++ GLFW3 输入处理

c# - Windows Mobile 6 紧凑框架的线程化

jquery - 为什么 jQuery 不插入基于属性选择器的新 html?

Python:如何使对象属性引用调用方法

c++ - 如何将文本从编辑控件设置到窗口

c++ - 如何将 `void(*fn)(const char *, ...)` 转换为 `std::function`,反之亦然

c# - 如何避免重复的 try catch block

c++ - 为什么 LD_PRELOAD 不适用于加载的共享库之一?