相关疑难解决方法(0)

如何将[[nodiscard]]属性应用于lambda?

我想阻止人们在不处理返回值的情况下调用lambda.

Clang 4.0拒绝我尝试过的所有内容,使用-std = c ++ 1z进行编译:

auto x = [&] [[nodiscard]] () { return 1; };
// error: nodiscard attribute cannot be applied to types
Run Code Online (Sandbox Code Playgroud)
auto x = [[nodiscard]] [&]() { return 1; };
// error: expected variable name or 'this' in lambda capture list
Run Code Online (Sandbox Code Playgroud)
auto x [[nodiscard]] = [&]() { return 1; };
// warning: nodiscard attribute only applies to functions, methods, enums, and classes
Run Code Online (Sandbox Code Playgroud)
[[nodiscard]] auto x = [&]() { return 1; };
// warning: nodiscard attribute …
Run Code Online (Sandbox Code Playgroud)

c++ lambda attributes language-lawyer c++17

14
推荐指数
1
解决办法
1252
查看次数

标签 统计

attributes ×1

c++ ×1

c++17 ×1

lambda ×1

language-lawyer ×1