what is std::identity and how it is used?
Asked Answered
S

5

8

I just want to know what is the purpose of std::identity? I could not find anything useful on web. I know how it is implemented :

template <typename T>
struct identity
{
    T operator()(T x) const { return x; }
};

why do we actually need this?

Shooter answered 20/1, 2017 at 15:34 Comment(4)
Standard doesn't have std::identity, all proposals have been removed. You might consider this as a hint.Stipend
the main use of the identity function is to be able to pass an argument wrapped around a function pointer if this is the argument expected. It is common in functional programming and tends to be used a lot in languages like haskell. see: #25987585Darling
#15202974 read this and you'll get itDepict
then what is this? en.cppreference.com/w/cpp/utility/functional/identityBurseraceous
H
5

Other people already answered the question - it's useful for things like a default for a function-type template parameter, and for haskell-style functional programming.

But your example implementation is not correct. Your code would perform a value copy, which std::identity does not do - it perfectly forwards. It's also constexpr and is transparent.

So this is an example of how it would be implemented, I believe:

    struct identity
    {
        using is_transparent = void;

        template <typename T>
        constexpr T&& operator()(T&& t) const noexcept
        {
            return std::forward<T>(t);
        }
    };
Hoofed answered 31/12, 2020 at 11:30 Comment(0)
S
3

Standard (up to C++20) doesn't have std::identity, all proposals mentioning it have been removed. When initially suggested, it was supposed to serve the same purpose as std::forward serves in accepted standard, but it conflicted with non-standard extensions and after several iterations was finally removed.

C++20 has std::identity back: https://en.cppreference.com/w/cpp/utility/functional/identity

Stipend answered 20/1, 2017 at 15:40 Comment(4)
@MEMS, VS extension.Stipend
Could you provide an example with which extensions it conflicted? And how?Aristocrat
@Aristocrat - exactly in the comments above, VS extension!Stipend
C++20 adds std::identity. See en.cppreference.com/w/cpp/utility/functional/identitySixtyfourmo
P
2

The struct you have in your code is the identity function T -> T where T is a template parameter. This function isn't useful on its own, but may be useful in other contexts where you need to pass a function parameter and the identify function is the one you want insert there. It's generally useful as a sort-of "do nothing" function.

As to std::identity, I can find no evidence that this struct exists in the C++ standard library.

Platte answered 20/1, 2017 at 15:40 Comment(4)
It's pretty useful to exclude arguments from template argument deduction. Cppreference has an example at en.cppreference.com/w/cpp/language/template_argument_deductionWhipping
@Whipping Unless I'm mistaken, the article you linked to seems to be talking about the identity type transformation rather than the identity function though. Similar concept, but not quite the same thing at least as far as C++ is concerned.Platte
huh, indeed, now I also wonder what the Microsoft identity is for.Whipping
@Cubbi: indeed pretty handy. Boost clamp uses identity to prevent argument deduction for its other arguments but unfortunately std::clamp doesn't use that trick.Tireless
O
2

While not relevant at the time of the question being asked, C++20 adds std::identity which seem to come from Ranges proposal. Here is its previous definition from Ranges TS where the main usage of it is explained as:

It is used as the default projection for all Ranges TS algorithms.

Openhearth answered 1/3, 2019 at 8:25 Comment(0)
N
0

I don't have std::identity in my libraries, but it should be a useful tool to copy a vector into another one without the nullptr objects in it via std::copy_if.

Nonagon answered 23/5, 2017 at 19:36 Comment(1)
so you mean it is mainly used to prevent processing void pointers?Shooter

© 2022 - 2024 — McMap. All rights reserved.