Why does assigning std::ref not change the value of the referenced object?

A small modification to f2 provides the clue: template<class T> void f2(T arg) { arg.get() = xx; } This now does what you expect. This has happened because std::ref returns a std::reference_wrapper<> object. The assignment operator of which rebinds the wrapper. (see http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/operator%3D) It does not make an assignment to the wrapped reference. In the … Read more

What is the advantage of std::ref and std::reference_wrapper compared to regular references?

Well ref constructs an object of the appropriate reference_wrapper type to hold a reference to an object. Which means when you apply: auto r = ref(x); This returns a reference_wrapper and not a direct reference to x (ie T&). This reference_wrapper (ie r) instead holds T&. A reference_wrapper is very useful when you want to … Read more

What is the difference between std::reference_wrapper and a simple pointer?

std::reference_wrapper is useful in combination with templates. It wraps an object by storing a pointer to it, allowing for reassignment and copy while mimicking its usual semantics. It also instructs certain library templates to store references instead of objects. Consider the algorithms in the STL which copy functors: You can avoid that copy by simply … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)