Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

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

      If a function declarednodiscard or a function returning an enumeration or class declarednodiscard by value is called from adiscarded-value expression other than a cast tovoid, the compiler is encouraged to issue a warning.

      Contents

      [edit]Syntax

      [[nodiscard]] (1)(since C++17)
      [[nodiscard(string-literal)]] (2)(since C++20)
      string-literal - anunevaluated string literal that could be used to explain the rationale for why the result should not be discarded

      [edit]Explanation

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

      If, from adiscarded-value expression other than a cast tovoid,

      the compiler is encouraged to issue a warning.

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

      (since C++20)

      [edit]Example

      Run this code
      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 C++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 valueauto z= strategic_value(0,0);// OK: return value is not discardedreturn z;}

      Possible output:

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

      Standard library

      The following standard functions are declared withnodiscard attribute:

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

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P1771R1C++17[[nodiscard]] on constructors has no effectcan cause a warning if the constructed object is discarded

      [edit]References

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

      [edit]See also

      (C++11)
      placeholder to skip an element when unpacking atuple usingtie
      (constant)[edit]
      C documentation fornodiscard
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/attributes/nodiscard&oldid=181334"

      [8]ページ先頭

      ©2009-2025 Movatter.jp