transform, boost::lambda and subscript

M

mojmir

hello,

i have two vectors and i am combining them into third one:
std::vector<int> orig, result;
std::vector<size_t> perm; // permutations

but i cannot compile following line:
std::transform(perm.begin(), perm.end(), result.begin(), orig[_1]);

because compiler seems to have trouble selecting among:
reindex.cpp:43: error: no match for 'operator[]' in 'orig[_1]'
stl_vector.h:462: candidates are:
_Alloc::reference std::vector<..>::eek:perator[](size_t)
stl_vector.h:476:
_Alloc::const_reference std::vector<..>::eek:perator[](size_t) const

i've tried another form that works
std::transform(perm.begin(), perm.end(), result.begin(),
*(&orig[0]+_1));

can you please help me understand what is going on?

many thanks in advance,
mojmir
 
K

Kai-Uwe Bux

mojmir said:
hello,

i have two vectors and i am combining them into third one:
std::vector<int> orig, result;
std::vector<size_t> perm; // permutations

but i cannot compile following line:
std::transform(perm.begin(), perm.end(), result.begin(), orig[_1]);

because compiler seems to have trouble selecting among:
reindex.cpp:43: error: no match for 'operator[]' in 'orig[_1]'
stl_vector.h:462: candidates are:
_Alloc::reference std::vector<..>::eek:perator[](size_t)
stl_vector.h:476:
_Alloc::const_reference std::vector<..>::eek:perator[](size_t) const

i've tried another form that works
std::transform(perm.begin(), perm.end(), result.begin(),
*(&orig[0]+_1));

can you please help me understand what is going on?

The problem is caused by the language rule that operator[] must be a member
function. In order to turn an expression involving operator[] into a lambda
expression, the left hand operand needs to be "lambdafied" first. Try:

std::transform(perm.begin(), perm.end(), result.begin(), var(orig)[_1]);


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top