Namespaces
Variants
Views
Actions

std::identity

From cppreference.com
< cpp‎ | utility‎ | functional
Revision as of 20:56, 24 October 2020 by Space Mission (Talk | contribs)

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
identity
(C++20)
Transparent operator wrappers
(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)

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 whose operator() returns its argument unchanged.

Contents

Member types

Member type Definition
is_transparent /* unspecified */

Member functions

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).

Parameters

t - argument to return

Return value

std::forward<T>(t).

Notes

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.

Example

#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>
#include <string>
#include <utility>
#include <vector>
 
// A helper operator that prints the std::pair.
template <typename U, typename V>
std::ostream& operator<< (std::ostream& os, std::pair<U, V> const& p) {
    return os << "{ " << p.first << ", " << p.second << " }";
}
 
// A range-printer that can print projected (modified) elements of a range.
template <std::ranges::input_range R,
          typename Projection = std::identity> //<- Note 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<std::pair<int, std::string>> v{
        {1, "one"}, {2, "two"}, {3, "three"}
    };
 
    print("Print using std::identity as a projection: ", v);
    print("Project the std::pair::first: ", v, &std::pair<int, std::string>::first);
    print("Project the std::pair::second: ", v, &std::pair<int, std::string>::second);
    print("Print using custom closure as a projection: ", v,
        [](std::pair<int, std::string> const& p) {
            return std::to_string(p.first) + ':' + p.second;
        });
}

Output:

Print using std::identity as a projection: { { 1, one } { 2, two } { 3, three } }
Project the std::pair::first: { 1 2 3 }
Project the std::pair::second: { one two three }
Print using custom closure as a projection: { 1:one 2:two 3:three }