Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language‎ | attributes
 
 
 
 
Attributes
(C++14)
nodiscard
(C++17)
(C++20)(C++20)
(C++20)(C++20)(C++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.

Contents

[edit] Syntax

[[nodiscard]]

[edit] Explanation

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

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.

[edit] Example

struct [[nodiscard]] error_info { };
error_info enable_missile_safety_mode();
void launch_missiles();
void test_missiles() {
   enable_missile_safety_mode(); // compiler may warn on discarding a nodiscard value
   launch_missiles();
}
error_info& foo();
void f1() {
    foo(); // nodiscard type is not returned by value, no warning
}


[edit] Standard library

The following standard functions are declared with nodiscard attribute:

Allocation functions
allocation functions
(function) [edit]
allocates uninitialized storage
(public member function of std::allocator) [edit]
[static]
allocates uninitialized storage using the allocator
(public static member function of std::allocator_traits) [edit]
allocates memory
(public member function of std::pmr::memory_resource) [edit]
Allocate memory
(public member function of std::pmr::polymorphic_allocator) [edit]
allocates uninitialized storage using the outer allocator
(public member function of std::scoped_allocator_adaptor) [edit]
Emptiness-checking functions
(C++17)
checks whether the container is empty
(function template) [edit]
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) [edit]
checks whether the string is empty
(public member function of std::basic_string) [edit]
checks whether the view is empty
(public member function of std::basic_string_view) [edit]
checks whether the container is empty
(public member function of std::deque) [edit]
checks whether the container is empty
(public member function of std::forward_list) [edit]
checks whether the container is empty
(public member function of std::list) [edit]
checks whether the container is empty
(public member function of std::map) [edit]
checks whether the match was successful
(public member function of std::match_results) [edit]
checks whether the container is empty
(public member function of std::multimap) [edit]
checks whether the container is empty
(public member function of std::multiset) [edit]
checks whether the underlying container is empty
(public member function of std::priority_queue) [edit]
checks whether the underlying container is empty
(public member function of std::queue) [edit]
checks whether the container is empty
(public member function of std::set) [edit]
checks whether the underlying container is empty
(public member function of std::stack) [edit]
checks whether the container is empty
(public member function of std::unordered_map) [edit]
checks whether the container is empty
(public member function of std::unordered_multimap) [edit]
checks whether the container is empty
(public member function of std::unordered_multiset) [edit]
checks whether the container is empty
(public member function of std::unordered_set) [edit]
checks whether the container is empty
(public member function of std::vector) [edit]
checks if the path is empty
(public member function of std::filesystem::path) [edit]
Miscellaneous
(C++11)
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result
(function template) [edit]
(C++17)
pointer optimization barrier
(function template) [edit]