[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/utility/functional/identity [Back]  [Original]

std::identity cppreference.com
cppreference.com

std::identity

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
(C++20)(C++23)
(C++11)
(C++17)(C++23)
(C++20)
(C++11)(C++11)
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

( C++17*)
( C++17*)
( C++17*)( C++17*)( C++17*)( C++17*)
( C++20*)
( C++20*)
( C++17*)( C++17*)
( C++17*)( C++17*)

( C++17*)
( C++20*)
( C++20*)
 
<tbody> </tbody>
struct identity;
( C++20)

std::identity , operator() .

is_transparent /* */

-


(public -)

std::identity::operator()

<tbody> </tbody>
template< class T> constexpr T&& operator()( T&& t ) const noexcept;

std::forward<T>(t).

t

std::forward<T>(t).

is_transparent , : , , rvalue. , , std::set::find std::set::lower_bound , Compare.

std::identity . .

#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>

struct Pair {
    int n; std::string s;
    friend std::ostream& operator<< (std::ostream& os, const Pair& p) {
        return os << "{ " << p.n << ", " << p.s << " }";
    }
};

//  ,     ()
//  .
template <std::ranges::input_range R,
          typename Projection = std::identity> //<-    
                                               //  
void print(std::string_view const rem, R&& r, Projection proj = {}) {
    std::cout << rem << "{ ";
    std::ranges::for_each(r, [](const auto& o){ std::cout << o << ' '; }, proj);
    std::cout << "}\n";
}

int main()
{
    const std::vector<Pair> v{ {1, ""}, {2, ""}, {3, ""} };

    print("   std::identity   :\n ", v);
    print(" Pair::n: ", v, &Pair::n);
    print(" Pair::s: ", v, &Pair::s);
    print("       :\n ", v,
        [](Pair const& p) { return std::to_string(p.n) + ':' + p.s; });
}

:

   std::identity   :
 { { 1,  } { 2,  } { 3,  } }
 Pair::n: { 1 2 3 }
 Pair::s: {    }
       :
 { 1: 2: 3: }


( ) []

Web Proxy Viewer  |  New URL  |  Original Page