Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ignore

      From cppreference.com
      <cpp‎ |utility‎ |tuple
       
       
      Utilities library
       
       
      Defined in header<tuple>
      Defined in header<utility>
      (1)
      const/*ignore-type*/ ignore;
      (since C++11)
      (until C++14)
      constexpr/*ignore-type*/ ignore;
      (since C++14)
      (inline since c++17)
      (2)
      struct/*ignore-type*/

      {
         template<class T>
         const/*ignore-type*/& operator=(const T&)constnoexcept
         {
             return*this;
         }

      };
      (since C++11)
      (until C++14)
      (exposition only*)
      struct/*ignore-type*/

      {
         template<class T>
         constexprconst/*ignore-type*/& operator=(const T&)constnoexcept
         {
             return*this;
         }

      };
      (since C++14)
      (exposition only*)
      1) An object such that any value can be assigned to it with no effect.
      2) The type ofstd::ignore.

      Contents

      [edit]Notes

      Avoid expression or a volatile bit-field value cannot be assigned tostd::ignore.

      std::ignore is intended for use withstd::tie when unpacking astd::tuple, as a placeholder for the arguments that are not used, but can be used for any unwanted assignment.

      Some code guides recommend usingstd::ignore to avoid warnings from unused return values of[[nodiscard]] functions, even though an assignment isn't required.

      For ignoring values not requiring assignment, one may cast tovoid. For variables that have names, but whose value is unused, one may cast those tovoid or declare those variables with[[maybe_unused]].

      [edit]Example

      1. Demonstrates the use ofstd::ignore together with a[[nodiscard]] function.
      2. Unpacks astd::pair<iterator,bool> returned bystd::set::insert(), but only saves the boolean.
      Run this code
      #include <iostream>#include <set>#include <string>#include <tuple> [[nodiscard]]int dontIgnoreMe(){return42;} int main(){    std::ignore= dontIgnoreMe(); std::set<std::string> set_of_str;if(bool inserted{false};std::tie(std::ignore, inserted)= set_of_str.insert("Test"),        inserted)std::cout<<"Value was inserted successfully.\n";}

      Output:

      Value was inserted successfully.

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2773C++14std::tuple was madeconstexpr butstd::ignore was not yetmadeconstexpr
      P2968R2C++11the behavior ofstd::ignore outside ofstd::tie was not formally specifiedmade fully specified

      [edit]See also

      (C++11)
      creates atuple of lvalue references or unpacks a tuple into individual objects
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/tuple/ignore&oldid=178262"

      [8]ページ先頭

      ©2009-2025 Movatter.jp