Namespaces
Variants

C++ attribute: nodiscard (since C++17)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

如果从 被丢弃值表达式 中调用声明为 nodiscard 的函数,或调用返回以值形式声明为 nodiscard 的枚举或类的函数(转换为 void 的情况除外),编译器将建议发出警告。

目录

语法

[ [ nodiscard ] ] (1) (自 C++17 起)
[ [ nodiscard ( 字符串字面量 ) ] ] (2) (自 C++20 起)
string-literal - 一个 未求值字符串字面量 ,可用于解释结果不应被舍弃的理由

说明

出现在函数声明、枚举声明或类声明中。

如果,从一个 被丢弃值表达式 而非强制转换为 void 的表达式出发,

鼓励编译器发出警告。

如果指定了 string-literal ,通常会被包含在警告信息中。

(since C++20)

示例

struct [[nodiscard]] error_info { /*...*/ };
error_info enable_missile_safety_mode() { /*...*/ return {}; }
void launch_missiles() { /*...*/ }
void test_missiles()
{
    enable_missile_safety_mode(); // 编译器可能对丢弃 nodiscard 值发出警告
    launch_missiles();
}
error_info& foo() { static error_info e; /*...*/ return e; }
void f1() { foo(); } // 非按值返回 nodiscard 类型,无警告
// nodiscard( 字符串字面量 ) (C++20 起):
[[nodiscard("PURE FUN")]] int strategic_value(int x, int y) { return x ^ y; }
int main()
{
    strategic_value(4, 2); // 编译器可能对丢弃 nodiscard 值发出警告
    auto z = strategic_value(0, 0); // 正常:返回值未被丢弃
    return z;
}

可能的输出:

game.cpp:5:4: warning: ignoring return value of function declared with ⮠
 'nodiscard' attribute
game.cpp:17:5: warning: ignoring return value of function declared with ⮠
 'nodiscard' attribute: PURE FUN

标准库

以下标准函数使用 nodiscard 属性声明:

扩展内容
分配函数
分配函数
(函数)
分配未初始化的存储空间
( std::allocator<T> 的公开成员函数)
[static]
使用分配器分配未初始化的存储空间
( std::allocator_traits<Alloc> 的公开静态成员函数)
分配内存
( std::pmr::memory_resource 的公开成员函数)
分配内存
( std::pmr::polymorphic_allocator<T> 的公开成员函数)
使用外层分配器分配未初始化存储空间
( std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...> 的公开成员函数)
间接访问
(C++17)
指针优化屏障
(函数模板)
告知编译器指针已对齐
(函数模板)
空值检查函数
(C++17)
检查容器是否为空
(函数模板)
检查节点句柄是否为空
( node handle 的公开成员函数)
检查容器是否为空
( std::array<T,N> 的 公开成员函数)
检查字符串是否为空
std::basic_string<CharT,Traits,Allocator> 的公开成员函数)
检查视图是否为空
( std::basic_string_view<CharT,Traits> 的公开成员函数)
检查容器是否为空
std::deque<T,Allocator> 的公开成员函数)
检查容器是否为空
std::forward_list<T,Allocator> 的公开成员函数)
检查容器是否为空
( std::list<T,Allocator> 的公开成员函数)
检查容器是否为空
std::map<Key,T,Compare,Allocator> 的公开成员函数)
检查匹配是否成功
( std::match_results<BidirIt,Alloc> 的公开成员函数)
检查容器是否为空
( std::multimap<Key,T,Compare,Allocator> 的公开成员函数)
检查容器是否为空
( std::multiset<Key,Compare,Allocator> 的公开成员函数)
检查容器适配器是否为空
( std::priority_queue<T,Container,Compare> 的公开成员函数)
检查容器适配器是否为空
( std::queue<T,Container> 的公开成员函数)
检查容器是否为空
( std::set<Key,Compare,Allocator> 的公开成员函数)
检查序列是否为空
( std::span<T,Extent> 的公开成员函数)
检查容器适配器是否为空
( std::stack<T,Container> 的公开成员函数)
检查容器是否为空
( std::unordered_map<Key,T,Hash,KeyEqual,Allocator> 的公开成员函数)
检查容器是否为空
( std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator> 的公开成员函数)
检查容器是否为空
( std::unordered_multiset<Key,Hash,KeyEqual,Allocator> 的公开成员函数)
检查容器是否为空
( std::unordered_set<Key,Hash,KeyEqual,Allocator> 的公开成员函数)
检查容器是否为空
std::vector<T,Allocator> 的公开成员函数)
检查路径是否为空
( std::filesystem::path 的 公开成员函数)
杂项
(C++11)
异步运行函数(可能在新线程中)并返回一个 std::future 用于保存结果
(函数模板)
(直至 C++26)

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 适用版本 发布行为 正确行为
P1771R1 C++17 [[nodiscard]] 在构造函数上无效 当构造的对象被丢弃时可能引发警告

参考文献

  • C++23 标准 (ISO/IEC 14882:2024):
  • 9.12.9 Nodiscard 属性 [dcl.attr.nodiscard]
  • C++20 标准 (ISO/IEC 14882:2020):
  • 9.12.8 Nodiscard 属性 [dcl.attr.nodiscard]
  • C++17 标准 (ISO/IEC 14882:2017):
  • 10.6.7 Nodiscard 属性 [dcl.attr.nodiscard]

另请参阅

(C++11)
在解包 tuple 时用于跳过元素的占位符 tie
(常量)
C 文档 关于 nodiscard