Re: transform, make_pair, and rvalues
Am 12.04.2014 20:10, schrieb James K. Lowden:
Today we encountered an rvalue question I couldn't answer.
Array a;
Array64 b;
std::map<int, uint64_t> c;
transform( a.begin(), a.end(), b.begin(), inserter(c, c.begin()),
make_pair<int, uint64_t> );
The compilation error from VC11 said it couldn't convert parameter 2 of
make_pair from uint64_t&& to uint64_t. Bug or feature?
As others have pointed out, the template parameters of make_pair are
supposed to be deduced which involves some perfect forwarding magic
(possibly resulting in lvalue reference types for lvalues).
Just like overload resolution, template argument deduction is something
that one should not do manually in general. With a lambda expression you
can let the compiler worry about that:
transform( a.begin(), a.end(), b.begin(), inserter(c, c.begin()),
[](int x, uint64_t y){return make_pair(x,y);} );
This way, it will also be easier for the compiler to inline the
make_pair expression into the std::transform<> instantiation.
Cheers!
sg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]