mardi 16 août 2016

Transform predicate returning local variable by reference

In the following code, I am trying to copy a std::vector<char>, into a std::vector<uint8_t>. To avoid compiler warning, I am doing explicit casting inside the predicate of std::transform instead of using std::copy, like this

std::vector<char> buf(10, 10);
std::vector<uint8_t> u(10);
std::transform(std::begin(buf), std::end(buf), std::begin(u),                                 
     [](uint8_t val)->decltype(*std::begin(u)){                                            
     return static_cast<decltype (*std::begin(u))> (val);                              
     });    

For this code, I get compiler warning saying that I am trying to return a local variable val by reference.

Why is that and how can it be fixed?

Aucun commentaire:

Enregistrer un commentaire