Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/functional/identity"

From cppreference.com
< cpp‎ | utility‎ | functional
(+ example)
m (~)
 
(15 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
{{cpp/title|identity}}
 
{{cpp/title|identity}}
 
{{cpp/utility/functional/navbar}}
 
{{cpp/utility/functional/navbar}}
{{ddcl|header=functional|since=c++20|1=
+
{{ddcl|header=functional|since=c++20|
 
struct identity;
 
struct identity;
 
}}
 
}}
Line 9: Line 9:
 
===Member types===
 
===Member types===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Member type | Definition}}
+
{{dsc hitem|Type|Definition}}
{{dsc | {{tt|is_transparent}} | /* unspecified */}}
+
{{dsc|{{tt|is_transparent}}|[[cpp/utility/functional#Transparent function objects|unspecified]]}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem fun | cpp/utility/functional/identity | title=operator() | inlinemem=true | returns the argument unchanged}}
+
{{dsc mem fun|cpp/utility/functional/identity|title=operator()|inlinemem=true|returns the argument unchanged}}
 
{{dsc end}}
 
{{dsc end}}
  
{{member | {{small|std::identity::}}operator() | 2=
+
{{member|{{small|std::identity::}}operator()|2=
{{ddcl | 1=
+
{{ddcl|
template< class T>
+
template< class T >
 
constexpr T&& operator()( T&& t ) const noexcept;
 
constexpr T&& operator()( T&& t ) const noexcept;
 
}}
 
}}
Line 28: Line 28:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | t | argument to return}}
+
{{par|t|argument to return}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
 
{{c|std::forward<T>(t)}}.
 
{{c|std::forward<T>(t)}}.
 
 
}}
 
}}
  
 
===Notes===
 
===Notes===
{{cpp/utility/functional/is_transparent_note}}
+
{{tt|std::identity}} serves as the default projection in [[cpp/algorithm/ranges|constrained algorithms]]. Its direct usage is usually not needed.
  
 
===Example===
 
===Example===
Line 46: Line 45:
 
#include <ranges>
 
#include <ranges>
 
#include <string>
 
#include <string>
#include <utility>
 
#include <vector>
 
  
// A helper operator that prints the std::pair.
+
struct Pair
template <typename U, typename V>
+
{
std::ostream& operator<< (std::ostream& os, std::pair<U, V> const& p) {
+
    int n;
     return os << "{ " << p.first << ", " << p.second << " }";
+
    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.
 
// A range-printer that can print projected (modified) elements of a range.
template <std::ranges::input_range R,
+
template<std::ranges::input_range R,
          typename Projection = std::identity> //<- Note the default projection
+
        typename Projection = std::identity> //<- Notice the default projection
void print(std::string_view const rem, R&& r, Projection proj = {}) {
+
void print(std::string_view const rem, R&& range, Projection projection = {})
     std::cout << rem << "{ ";
+
{
     std::ranges::for_each(r, [](const auto& o){ std::cout << o << ' '; }, proj);
+
     std::cout << rem << '{';
 +
     std::ranges::for_each(
 +
        range,
 +
        [O = 0](const auto& o) mutable { std::cout << (O++ ? ", " : "") << o; },
 +
        projection
 +
    );
 
     std::cout << "}\n";
 
     std::cout << "}\n";
 
}
 
}
Line 66: Line 72:
 
int main()
 
int main()
 
{
 
{
    using namespace std::literals;
+
     const auto v = {Pair{1, "one"}, {2, "two"}, {3, "three"}<!---->};
     const std::vector<std::pair<int, std::string>> v{
+
        {1, "one"s}, {2, "two"s}, {3, "three"s}
+
    };
+
 
      
 
      
 
     print("Print using std::identity as a projection: ", v);
 
     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 Pair::n: ", v, &Pair::n);
     print("Project the std::pair::second: ", v, &std::pair<int, std::string>::second);
+
     print("Project the Pair::s: ", v, &Pair::s);
 
     print("Print using custom closure as a projection: ", v,
 
     print("Print using custom closure as a projection: ", v,
         [](std::pair<int, std::string> const& p) {
+
         [](Pair const& p) { return std::to_string(p.n) + ':' + p.s; });
            return std::to_string(p.first) + ':' + p.second;
+
        });
+
 
}
 
}
| output=
+
|output=
Print using std::identity as a projection: { { 1, one } { 2, two } { 3, three } }
+
Print using std::identity as a projection: {<!---->{1, one}, {2, two}, {3, three}<!---->}
Project the std::pair::first: { 1 2 3 }
+
Project the Pair::n: {1, 2, 3}
Project the std::pair::second: { one two three }
+
Project the Pair::s: {one, two, three}
Print using custom closure as a projection: { 1:one 2:two 3:three }
+
Print using custom closure as a projection: {1:one, 2:two, 3:three}
 
}}
 
}}
  
{{langlinks|ja|zh}}
+
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/types/dsc type_identity}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|es|ja|ru|zh}}

Latest revision as of 20:39, 13 July 2024

 
 
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

[edit] Member types

Type Definition
is_transparent unspecified

[edit] 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).

[edit] Notes

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

[edit] Example

#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>
#include <string>
 
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&& range, Projection projection = {})
{
    std::cout << rem << '{';
    std::ranges::for_each(
        range,
        [O = 0](const auto& o) mutable { std::cout << (O++ ? ", " : "") << o; },
        projection
    );
    std::cout << "}\n";
}
 
int main()
{
    const auto 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,
        [](Pair const& p) { return std::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]