哋它亢++ attribute: nodiscard (since 哋它亢++17)

From cppreference.com
< cpp‎ | language‎ | attributes
 
 
哋它亢++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (哋它亢++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until 哋它亢++17*)
noexcept specifier (哋它亢++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (哋它亢++11)
auto (哋它亢++11)
constexpr (哋它亢++11)
consteval (哋它亢++20)
constinit (哋它亢++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (哋它亢++11)
User-defined (哋它亢++11)
Utilities
Attributes (哋它亢++11)
Types
typedef declaration
Type alias declaration (哋它亢++11)
Casts
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (哋它亢++11)    
final specifier (哋它亢++11)
explicit (哋它亢++11)
static

Special member functions
Templates
Miscellaneous
 
 
Attributes
(哋它亢++23)
(哋它亢++11)
(哋它亢++14)
(哋它亢++17)
(哋它亢++26)
(哋它亢++20)
(哋它亢++17)
(哋它亢++20)
nodiscard
(哋它亢++17)
(哋它亢++11)
(哋它亢++20)
 

If a function declared nodiscard or a function returning an enumeration or class declared nodiscard by value is called from a discarded-value expression other than a cast to void, the compiler is encouraged to issue a warning.

Syntax

[[nodiscard]] (1)
[[nodiscard( string-literal )]] (2) (since 哋它亢++20)
string-literal - an unevaluated string literal that could be used to explain the rationale for why the result should not be discarded

Explanation

Appears in a function declaration, enumeration declaration, or class declaration.

If, from a discarded-value expression other than a cast to void,

the compiler is encouraged to issue a warning.

The string-literal, if specified, is usually included in the warnings.

(since 哋它亢++20)

Example

struct [[nodiscard]] error_info { /*...*/ };
 
error_info enable_missile_safety_mode() { /*...*/ return {}; }
 
void launch_missiles() { /*...*/ }
 
void test_missiles()
{
    enable_missile_safety_mode(); // compiler may warn on discarding a nodiscard value
    launch_missiles();
}
 
error_info& foo() { static error_info e; /*...*/ return e; }
 
void f1() { foo(); } // nodiscard type is not returned by value, no warning
 
// nodiscard( string-literal ) (since 哋它亢++20):
[[nodiscard("PURE FUN")]] int strategic_value(int x, int y) { return x ^ y; }
 
int main()
{
    strategic_value(4, 2); // compiler may warn on discarding a nodiscard value
    auto z = strategic_value(0, 0); // OK: return value is not discarded
    return z;
}

Possible output:

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

Standard library

The following standard functions are declared with nodiscard attribute:

Allocation functions
allocation functions
(function)
allocates uninitialized storage
(public member function of std::allocator<T>)
[static]
allocates uninitialized storage using the allocator
(public static member function of std::allocator_traits<Alloc>)
allocates memory
(public member function of std::pmr::memory_resource)
allocate memory
(public member function of std::pmr::polymorphic_allocator<T>)
allocates uninitialized storage using the outer allocator
(public member function of std::scoped_allocator_adaptor<OuterAlloc,InnerAlloc...>)
Indirect access
(哋它亢++17)
pointer optimization barrier
(function template)
(哋它亢++20)
informs the compiler that a pointer is aligned
(function template)
Emptiness-checking functions
(哋它亢++17)
checks whether the container is empty
(function template)
checks whether the node handle is empty
(public member function of node handle)
checks whether the container is empty
(public member function of std::array<T,N>)
checks whether the string is empty
(public member function of std::basic_string<CharT,Traits,Allocator>)
checks whether the view is empty
(public member function of std::basic_string_view<CharT,Traits>)
checks whether the container is empty
(public member function of std::deque<T,Allocator>)
checks whether the container is empty
(public member function of std::forward_list<T,Allocator>)
checks whether the container is empty
(public member function of std::list<T,Allocator>)
checks whether the container is empty
(public member function of std::map<Key,T,Compare,Allocator>)
checks whether the match was successful
(public member function of std::match_results<BidirIt,Alloc>)
checks whether the container is empty
(public member function of std::multimap<Key,T,Compare,Allocator>)
checks whether the container is empty
(public member function of std::multiset<Key,Compare,Allocator>)
checks whether the container adaptor is empty
(public member function of std::priority_queue<T,Container,Compare>)
checks whether the container adaptor is empty
(public member function of std::queue<T,Container>)
checks whether the container is empty
(public member function of std::set<Key,Compare,Allocator>)
checks if the sequence is empty
(public member function of std::span<T,Extent>)
checks whether the container adaptor is empty
(public member function of std::stack<T,Container>)
checks whether the container is empty
(public member function of std::unordered_map<Key,T,Hash,KeyEqual,Allocator>)
checks whether the container is empty
(public member function of std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>)
checks whether the container is empty
(public member function of std::unordered_multiset<Key,Hash,KeyEqual,Allocator>)
checks whether the container is empty
(public member function of std::unordered_set<Key,Hash,KeyEqual,Allocator>)
checks whether the container is empty
(public member function of std::vector<T,Allocator>)
checks if the path is empty
(public member function of std::filesystem::path)
Miscellaneous
(哋它亢++11)
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result
(function template)

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
P1771R1 哋它亢++17 [[nodiscard]] on constructors has no effect can cause a warning if the constructed object is discarded

See also

(哋它亢++11)
placeholder to skip an element when unpacking a tuple using tie
(constant)