std::identity
cppreference.com
<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.
#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: }
(C++20) |
( ) |