GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::identity(3) C++ Standard Libary std::identity(3)

std::identity - std::identity


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


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


Member type Definition
is_transparent /* unspecified */


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

std::identity::operator()


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


Returns std::forward<T>(t).


t - argument to return


std::forward<T>(t).


The member type is_transparent indicates to the caller that this function object is
a transparent function object: it accepts arguments of arbitrary types and uses
perfect forwarding, which avoids unnecessary copying and conversion when the
function object is used in heterogeneous context, or with rvalue arguments. In
particular, template functions such as std::set::find and std::set::lower_bound make
use of this member type on their Compare types.


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

// Run this code


#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 << " }";
}
};


// 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 projection
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, "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,
[](Pair const& p) { return std::to_string(p.n) + ':' + p.s; });
}


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 }


type_identity returns the type argument unchanged
(C++20) (class template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.