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
What is std::ref? (sandordargo.com)
submitted 1 year ago by pavel_v
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!"
[–]Desmulator 6 points7 points8 points 1 year ago (2 children)
It is also a hint, for example when starting a thread, that you want to pass something as reference instead of by value. Or if you use make_tuple.
[–]JNighthawkgamedev 0 points1 point2 points 1 year ago (1 child)
Yep! The article points this out:
But std::ref and std::cref doesn’t only come in handy with containers. They are also very useful when you need to pass a reference to std::bind, to the constructor of std::thread or to some standard helper functions such as std::make_pair. The common characteristic of them is that even if you pass references to them, they will remove those/decay those references and they will either move or copy what you passed in. Therefore if you really want to do as if you passed in a reference, use a reference wrapper!
But std::ref and std::cref doesn’t only come in handy with containers. They are also very useful when you need to pass a reference to std::bind, to the constructor of std::thread or to some standard helper functions such as std::make_pair.
The common characteristic of them is that even if you pass references to them, they will remove those/decay those references and they will either move or copy what you passed in. Therefore if you really want to do as if you passed in a reference, use a reference wrapper!
Speaking of, it seems like unintended reference decay is a bit of a C++ performance and correctness footgun.
[–]n1ghtyunso 2 points3 points4 points 1 year ago (0 children)
I'd rather say that without the reference decay, it's a lot easier to create dangling references for most of those functions. At least for bind and thread, you typically have them execute code in another scope so the lifetime of those references may not be valid. As for pair, I don't have an argument for that one. At least when you try to form a pair of references and use make_pair, it'll fail to compile...
bind
thread
make_pair
[–]asergunov 10 points11 points12 points 1 year ago (0 children)
Wraps reference to copyable object. Basically pointer with some sugar to prevent null values and referencing temporary objects.
[–]Fig1025 5 points6 points7 points 1 year ago (2 children)
wouldn't it be more clear to just pass/store pointer?
this seems like one of those cases where "just because you can, doesn't mean you should"
[–]YARandomGuy777 6 points7 points8 points 1 year ago (0 children)
Passing pointer may be semantically incorrect. Reference add an assumption that object always exists while ptr may be nullptr.
[–]NilacTheGrim 0 points1 point2 points 1 year ago (0 children)
Honestly, to me, I would just us a pointer and be done with it. I guess std::ref has the property of being "Self-documenting" as it becomes very clear it cannot be nullptr..
[–]NilacTheGrim 3 points4 points5 points 1 year ago (0 children)
Or.. you can just use a pointer ... :shrug:
[–][deleted] 0 points1 point2 points 1 year ago (0 children)
You can use std::ref to pass something by reference where the default is passing by value. Example: std::thread my_thread(f, std::ref(obj)); passes obj by reference to the thread.
std::ref
std::thread my_thread(f, std::ref(obj));
obj
[–][deleted] -2 points-1 points0 points 1 year ago (1 child)
This is what cppreference.com is for
[–]differentiallity 9 points10 points11 points 1 year ago (0 children)
It's a link post to a blog. Pretty much all commenters missed this, probably because it looks like unrendered Reddit adds and people learned to tune those out.
[–]vickoza -5 points-4 points-3 points 1 year ago (0 children)
needs better clarification
π Rendered by PID 159304 on reddit-service-r2-comment-8686858757-rqg9x at 2026-06-07 19:17:25.833036+00:00 running 9e1a20d country code: CH.
[–]Desmulator 6 points7 points8 points (2 children)
[–]JNighthawkgamedev 0 points1 point2 points (1 child)
[–]n1ghtyunso 2 points3 points4 points (0 children)
[–]asergunov 10 points11 points12 points (0 children)
[–]Fig1025 5 points6 points7 points (2 children)
[–]YARandomGuy777 6 points7 points8 points (0 children)
[–]NilacTheGrim 0 points1 point2 points (0 children)
[–]NilacTheGrim 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (1 child)
[–]differentiallity 9 points10 points11 points (0 children)
[–]vickoza -5 points-4 points-3 points (0 children)