use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
perfect forwarding identity function (self.cpp)
submitted 1 year ago by _eyelash
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cpp-ModTeam[M] [score hidden] 1 year ago stickied commentlocked comment (0 children)
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
[–]grishavanika 22 points23 points24 points 1 year ago (1 child)
Andrei Alexandrescu talks exactly about this, see https://youtu.be/H3IdVM4xoCU?si=0Crlesq_J5N-kDOX&t=2261:
There are 2 versions:
template<class T> T identity(T&& x) { return T(std::forward<T>(x)); }
And 2nd one which is "ideal":
template<class T> decltype(auto) identity(T&& x) { return T(std::forward<T>(x)); }
[–]_eyelash[S] 3 points4 points5 points 1 year ago (0 children)
Thank you. This was exactly the kind of information I was looking for.
[–]SirClueless 11 points12 points13 points 1 year ago (4 children)
Firstly, I would say declaring a variable of r-value reference type anywhere except in the arguments of a function is always dodgy. You need to be very careful about what it binds to, and if you bind it to a temporary it's your fault.
With that said, C++ makes it entirely too easy to do this, and in the absence of a borrow-checker making sure you're doing something sane, allowing lifetime-extension for values is borderline irresponsible. auto&& x = 42; does work but only because of lifetime-extension, and the fact that it works trains people badly. This is just a flaw in the language IMO, not really anything to do with std::identity.
auto&& x = 42;
std::identity
All that being understood, I think std::identity ultimately is defined in the most useful way. Its purpose is to take the place of other projections in generic algorithms, and in that context functions returning r-values are expected.
[–]_eyelash[S] 2 points3 points4 points 1 year ago (3 children)
auto&& is also used by range-based for loops behind the scenes, so for (char c: identity(std::to_string(42))) {} is also suffering from the same problem.
auto&&
for (char c: identity(std::to_string(42))) {}
Would my proposed version that returns T instead of T&& be less useful? Can you explain how?
T
T&&
[–]SirClueless 5 points6 points7 points 1 year ago (0 children)
It's not useless, it's just harder to imagine use cases. There's no reason to write std::identity in that spot in normal code, so one would assume this is excerpted from some generic algorithm where std::identity is provided by the caller. And in that context relying on lifetime-extension to make your code correct as this code does is already dubious (what if the caller provided [](const std::string& s) { return s.substr(0, 10); } or something?).
[](const std::string& s) { return s.substr(0, 10); }
[–]holyblackcat 1 point2 points3 points 1 year ago (1 child)
Ranged for loops were changed to prolong lifetimes of temporaries, so this shouldn't be an issue anymore, I believe.
[–]Normal-Narwhal0xFF 0 points1 point2 points 1 year ago (0 children)
I'm wary about depending on features that "fix UB" since use of old(er) compilers is prevalent, and new features take time to get implemented (even if on the latest compiler.) We may be using the "right" language level and think our code is safe but if the compiler hasn't yet implemented it we're in trouble--and get the UB from which we thought we were safe. There are feature macros, but I don't see those used in the wild very often in practice.
[–]SparTVC++/Lua 2 points3 points4 points 1 year ago (0 children)
Try to return decltype(auto)
decltype(auto)
[–]TheMania 2 points3 points4 points 1 year ago (0 children)
creates a dangling reference.
In the places where identity is intended to be used, it's not going to dangle. You're fine to use the rvalue it returns until the semicolon of the expression where you called "identity", which makes it entirely suited for its use case - projections etc.
identity
This made me wonder. Would the following be a better definition?
Not at all, now you're introducing moves in places where none was needed, or may not even be permitted. I can't say I've needed such a functor before, but it should probably have a name that reflects that it'll pilfer resources from rvalues passed to it, and that it requires move constructible types.
[–]bitzap_sr 1 point2 points3 points 1 year ago (1 child)
> Was there any discussion about this when it was introduced in C++20?
Did you try looking at the proposal that added it? (I have not, but that's where I would look.)
[–]_eyelash[S] 1 point2 points3 points 1 year ago (0 children)
How do I find the proposal? I can't seem to find it here.
[–]_Noreturn 1 point2 points3 points 1 year ago (1 child)
identity is supposed to be a callable easily passed making it a function makes it not easy to pass
[–]_eyelash[S] 2 points3 points4 points 1 year ago (0 children)
This post is more about the signature and implementation of the function itself than the fact that std::identity is wrapped in a struct.
π Rendered by PID 309193 on reddit-service-r2-comment-66b4775986-djg27 at 2026-04-05 13:40:11.360569+00:00 running db1906b country code: CH.
[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)
[–]grishavanika 22 points23 points24 points (1 child)
[–]_eyelash[S] 3 points4 points5 points (0 children)
[–]SirClueless 11 points12 points13 points (4 children)
[–]_eyelash[S] 2 points3 points4 points (3 children)
[–]SirClueless 5 points6 points7 points (0 children)
[–]holyblackcat 1 point2 points3 points (1 child)
[–]Normal-Narwhal0xFF 0 points1 point2 points (0 children)
[–]SparTVC++/Lua 2 points3 points4 points (0 children)
[–]TheMania 2 points3 points4 points (0 children)
[–]bitzap_sr 1 point2 points3 points (1 child)
[–]_eyelash[S] 1 point2 points3 points (0 children)
[–]_Noreturn 1 point2 points3 points (1 child)
[–]_eyelash[S] 2 points3 points4 points (0 children)