Why in C++ presented two functions with different names ref
and cref
? Why not the only overloaded ref
function? Are there some important semantic reasons?
An std::reference_wrapper is a copyable and assignable object that emulates a reference. Contrary to its name, it does not wrap a reference. It works by encapsulating a pointer (T*) and by implicitly converting to a reference (T&).
The cref() is a built-in function of the C++ programming language provided in the reference_wrapper standard library. It is a helper function that uses the template argument deduction for generating an object of reference_wrapper type while determining the result's template argument.
Because sometimes you want to wrap a const
reference to a non-const
object. In such cases, std::cref
allows you to write
std::cref(x)
instead of
std::ref(static_cast<const T&>(x))
(where T
is of course the type of x
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With