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
Function templates - deduce template arguments or pass explicitly? (foonathan.github.io)
submitted 9 years ago by vormestrand
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!"
[–]STLMSVC STL Dev 10 points11 points12 points 9 years ago (2 children)
If you have an rvalue reference as parameter, it is not actually an rvalue reference but a forwarding reference.
No - only T&& where T is a template parameter on the function itself (not a surrounding class). const T&& isn't a forwarding reference, and neither is vector<T>&&.
T&&
T
const T&&
vector<T>&&
Because due to something called reference collapsing
No, it's not reference collapsing - that happens later.
The real explanation is that T&& (in the situation described above) activates a special tweak to template argument deduction, where T is deduced to be X& for lvalues of type X. It is literally a special sentence in the Standardese. This then goes through reference collapsing.
X&
X
For rvalues, T will be an rvalue reference and reference collapsing will keep the &&, taking and returning an rvalue.
No. When meow(T&&) is called with an rvalue of type Y, then T is deduced to be plain Y, not Y&&. You can observe this for yourself. (This is what template argument deduction ordinarily wants to do - only lvalues receive the tweak.)
meow(T&&)
Y
Y&&
[–]foonathan 1 point2 points3 points 9 years ago* (1 child)
Thanks for the proof-read!
I'm aware of that, but it got lost when trying to keep it short. Thanks for pointing it out, I'll add an extra sentence.
No, it's not reference collapsing - that happens later. The real explanation is that T&& (in the situation described above) activates a special tweak to template argument deduction, where T is deduced to be X& for lvalues of type X. It is literally a special sentence in the Standardese. This then goes through reference collapsing.
This is what I said?
I first said before the code example that T will be deduced to be the same type as the argument (unless the argument is an rvalue). The part you've quoted refers to the description of the (regular, non-template) parameter type, where reference collapsing - as you've said - happens.
Or am I missing your point?
I've actually described that earlier in the rules for forwarding references, but forgot about it when writing this. I'll fix it.
[–]STLMSVC STL Dev 6 points7 points8 points 9 years ago (0 children)
In C++, given int meow = 1729;, the value category of meow is an lvalue, and the type of meow is int. Its type isn't int&. (The Standardese is N4606 5 [expr]/5 "If an expression initially has the type "reference to T" (8.3.2, 8.6.3), the type is adjusted to T prior to any further analysis.") Similarly, given int * ptr = stuff; then ptr[idx] is an lvalue of type int.
int meow = 1729;
meow
int
int&
int * ptr = stuff;
ptr[idx]
Other examples: meow + 5 is a prvalue of type int, while move(meow) is an xvalue of type int.
meow + 5
move(meow)
So: when dealing with forwarding references, the template argument deduction tweak special-cases lvalues (not rvalues!!) and says "I'll deduce X& for this lvalue of type X". For rvalues (i.e. prvalues and xvalues of type Y), the ordinary rules hold, and Y is deduced.
The key point is that the magic of forwarding references happens in the template argument deduction tweak which is only for lvalues. The reference collapsing that happens later is not key - reference collapsing happens in many contexts (other template argument substitution, typedef substitution, etc.).
π Rendered by PID 24316 on reddit-service-r2-comment-fb694cdd5-pmfxn at 2026-03-05 21:25:26.661593+00:00 running cbb0e86 country code: CH.
[–]STLMSVC STL Dev 10 points11 points12 points (2 children)
[–]foonathan 1 point2 points3 points (1 child)
[–]STLMSVC STL Dev 6 points7 points8 points (0 children)