How do you use std::transform in conjunction with containers which hold objects of type std::reference_wrapper? when reading from const std::vector<std::reference_wrapper<const float>> and writing into an std::vector<float> everything works, but if I try to write into an std::vector<std::reference_wrapper<float>> it doesn't compile giving the error
/usr/include/c++/5/bits/stl_algo.h:4214: error: use of deleted function >'std::reference_wrapper<_Tp>::reference_wrapper(_Tp&&) [with _Tp = float]' __result = __binary_op(__first1, *__first2);
I think it is trying to replace the reference instead of changing the value of the referred value. Is there a way to use transform to achieve this or should I prefer writing my own function?
edit: added a example for reference:
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
int main()
{
std::vector<float> v(3, 1.0f);
std::vector<std::reference_wrapper<float>> refVector;
refVector.reserve(v.size());
for(auto& elem : v)
{
refVector.push_back((std::reference_wrapper<float>)elem);
}
std::vector<float> v2(3, 2.0f);
std::vector<float> v3(3, 3.0f);
std::vector<float> v4(3);
for(auto& elem : v)
std::cout << elem << std::endl; // all 1s
std::transform(refVector.begin(), refVector.end(), v3.begin(), v4.begin(), std::plus<float>());
for(auto& elem : v4)
std::cout << elem << std::endl; // all 4s
std::transform(v2.begin(), v2.end(), v3.begin(), refVector.begin(), std::minus<float>()); // doesn't compile
for(auto& elem : v)
std::cout << elem << std::endl; // want all -1s
return 0;
}
3.14. So you can't put3.14directly into a reference wrapper, but you can put a reference to a variable there.getmember function for getting the reference.