Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::identity

      From cppreference.com
      <cpp‎ |utility‎ |functional
       
       
      Utilities library
       
      Function objects
      Function invocation
      (C++17)(C++23)
      Identity function object
      identity
      (C++20)
      Old binders and adaptors
      (until C++17*)
      (until C++17*)
      (until C++17*)
      (until C++17*)  
      (until C++17*)
      (until C++17*)(until C++17*)(until C++17*)(until C++17*)
      (until C++20*)
      (until C++20*)
      (until C++17*)(until C++17*)
      (until C++17*)(until C++17*)

      (until C++17*)
      (until C++17*)(until C++17*)(until C++17*)(until C++17*)
      (until C++20*)
      (until C++20*)
       
      Defined in header<functional>
      struct identity;
      (since C++20)

      std::identity is a function object type whoseoperator() returns its argument unchanged.

      Contents

      [edit]Member types

      Type Definition
      is_transparentunspecified

      [edit]Member functions

      operator()
      returns the argument unchanged
      (public member function)

      std::identity::operator()

      template<class T>
      constexpr T&& operator()( T&& t)constnoexcept;

      Returnsstd::forward<T>(t).

      Parameters

      t - argument to return

      Return value

      std::forward<T>(t).

      [edit]Notes

      std::identity serves as the default projection inconstrained algorithms. Its direct usage is usually not needed.

      [edit]Example

      Run this code
      #include <algorithm>#include <functional>#include <iostream>#include <ranges>#include <string> struct Pair{int n;std::string s;friendstd::ostream& operator<<(std::ostream& os,const Pair& p){return os<<'{'<< p.n<<", "<< p.s<<'}';}}; // A range-printer that can print projected (modified) elements of a range.template<std::ranges::input_range R,typename Projection= std::identity>//<- Notice the default projectionvoid print(std::string_viewconst rem, R&& range, Projection projection={}){std::cout<< rem<<'{';    std::ranges::for_each(        range,[O=0](constauto& o) mutable{std::cout<<(O++?", ":"")<< o;},        projection);std::cout<<"}\n";} int main(){constauto v={Pair{1,"one"},{2,"two"},{3,"three"}};     print("Print using std::identity as a projection: ", v);    print("Project the Pair::n: ", v,&Pair::n);    print("Project the Pair::s: ", v,&Pair::s);    print("Print using custom closure as a projection: ", v,[](Pairconst& p){returnstd::to_string(p.n)+':'+ p.s;});}

      Output:

      Print using std::identity as a projection: {{1, one}, {2, two}, {3, three}}Project the Pair::n: {1, 2, 3}Project the Pair::s: {one, two, three}Print using custom closure as a projection: {1:one, 2:two, 3:three}

      [edit]See also

      returns the type argument unchanged
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/identity&oldid=173614"

      [8]ページ先頭

      ©2009-2025 Movatter.jp