#Assigning a std::function from the functional library to the identity

19 messages · Page 1 of 1 (latest)

leaden bramble
#

so there's supposed to be a std::identity in functional, but I can't figure out how to assign it to a std::function

#include <functional>
#include <iostream>

int main()
{
    long double val = 20;
    std::function<long double(long double)> f;

    f = std::identity;

    std::cout << f(val);
    
    return 0;
}```
This is the error I get `namespace "std" has no member "identity"`
But the console should output `20`, of course
crystal karmaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

lone quest
#

!cppref identity

crystal karmaBOT
lone quest
#

identity is a type, not an object

leaden bramble
#

would the best solution just be to write my own identity function, or is there a std object that does that

#

ofc it'd be very easy, but std is standard yk

lone quest
#

Try
std::identity{}

#

Like create an identity object

leaden bramble
#

Like this?

int main()
{
    long double val = 20;
    std::function<long double(long double)> f;

    f = std::identity{};

    cout << f(val);
    
    return 0;
}

I get these errors
'identity': is not a member of 'std'
'identity': undeclared identifier
expected a ';'
namespace "std" has no member "identity"

torpid panther
#

;compile -includeiostream -includefunctional -std=c++20

int main()
{
    long double val = 20;
    std::function<long double(long double)> f;

    f = std::identity{};

    std::cout << f(val);
    
    return 0;
}
tender stagBOT
#
Program Output
20
leaden bramble
#

oh thats cool, didn't know we could do that

torpid panther
#

do you got it?

leaden bramble
#

yesss

#

thank yall

#

!solved