C++ nodiscard
nodiscard
用于标记函数的返回值:
[[nodiscard]] int Compute();
当调用该函数却不赋值返回结果时,将收到警告:
1 | void Foo() { |
1 | warning: ignoring return value of 'int Compute()', declared with attribute nodiscard |
标记整个类型
1 | [[nodiscard]] |
每当调用任何返回ImportantType
的函数时,都会收到警告。
使用注意
过多使用该关键字可能导致编译器编译时出现大量warning
。
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.