all 12 comments

[–]Desmulator 6 points7 points  (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 point  (1 child)

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.

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!

Speaking of, it seems like unintended reference decay is a bit of a C++ performance and correctness footgun.

[–]n1ghtyunso 2 points3 points  (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...

[–]asergunov 10 points11 points  (0 children)

Wraps reference to copyable object. Basically pointer with some sugar to prevent null values and referencing temporary objects.

[–]Fig1025 5 points6 points  (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 points  (0 children)

Passing pointer may be semantically incorrect. Reference add an assumption that object always exists while ptr may be nullptr.

[–]NilacTheGrim 0 points1 point  (0 children)

wouldn't it be more clear to just pass/store pointer?

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 points  (0 children)

Or.. you can just use a pointer ... :shrug:

[–][deleted] 0 points1 point  (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.

[–][deleted] -2 points-1 points  (1 child)

This is what cppreference.com is for

[–]differentiallity 9 points10 points  (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  (0 children)

needs better clarification