Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL ref and cref functions

Tags:

c++

stl

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?

like image 869
Tomilov Anatoliy Avatar asked Oct 29 '14 08:10

Tomilov Anatoliy


People also ask

What does std :: ref do?

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&).

What is CREF C++?

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.


1 Answers

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).

like image 123
Brian Bi Avatar answered Sep 22 '22 05:09

Brian Bi