9

I have a vector of pointers. I would like to call a function for every element, but that function takes a reference. Is there a simple way to dereference the elements?

Example:

MyClass::ReferenceFn( Element & e ) { ... }

MyClass::PointerFn( Element * e ) { ... }

MyClass::Function()
{
    std::vector< Element * > elements;
    // add some elements...

    // This works, as the argument is a pointer type
    std::for_each( elements.begin(), elements.end(),
                   boost::bind( &MyClass::PointerFn, boost::ref(*this), _1 ) );

    // This fails (compiler error), as the argument is a reference type
    std::for_each( elements.begin(), elements.end(),
                   boost::bind( &MyClass::ReferenceFn, boost::ref(*this), _1 ) );
}

I could create a dirty little wrapper that takes a pointer, but I figured there had to be a better way?

1
  • Is there a reason you're using boost::ref(*this)? I just use: boost::bind(&MyClass::ReferenceFn, this, _1) and it works fine. Commented Sep 21, 2010 at 11:57

2 Answers 2

15

You could use boost::indirect_iterator:

std::for_each( boost::make_indirect_iterator(elements.begin()), 
               boost::make_indirect_iterator(elements.end()),
               boost::bind( &MyClass::ReferenceFn, boost::ref(*this), _1 ) );

That will dereference the adapted iterator twice in its operator*.

Sign up to request clarification or add additional context in comments.

4 Comments

+1, although for this case I prefer BOOST_FOREACH(Element *e, elements) this->ReferenceFn(*e);. C++ may be usable as a functional language, but not as a concise functional language...
And the Python would be for e in elements: self.ReferenceFn(e). It's heart-breaking.
For C++0x it will be for(auto *e : elements) ReferenceFn(*e);. Sweet :)
I'm considering starting to call it C++JamTomorrow.
3

It looks like you could also use the Boost.Lambda library.

// Appears to compile with boost::lambda::bind
    using namespace boost::lambda;
    std::for_each( elements.begin(), elements.end(),
                   bind( &MyClass::ReferenceFn, boost::ref(*this), *_1 ) );

But I agree with the commenters about preferring BOOST_FOREACH. The for_each "algorithm" does practically nothing useful, and what it does, range-based for loop can do for you with a much smaller effort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.