transform, boost::lambda and subscript

Discussion in 'C++' started by mojmir, Aug 30, 2008.

  1. mojmir

    mojmir Guest

    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
     
    mojmir, Aug 30, 2008
    #1
    1. Advertising

  2. mojmir

    Kai-Uwe Bux Guest

    mojmir wrote:

    > 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
     
    Kai-Uwe Bux, Aug 31, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Suzi
    Replies:
    13
    Views:
    608
    Bengt Richter
    Jan 7, 2005
  2. Toby Bradshaw
    Replies:
    6
    Views:
    1,756
    Kai-Uwe Bux
    Jun 2, 2006
  3. Misiu
    Replies:
    3
    Views:
    2,389
    Misiu
    Jan 31, 2007
  4. Replies:
    0
    Views:
    591
  5. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    178
    Sean O'Halpin
    Mar 30, 2009
Loading...

Share This Page