
On Wed, Aug 26, 2009 at 3:44 PM, Steven Watanabe<watanabesj@gmail.com> wrote:
AMDG
Robert Jones wrote:
I've asked a similar question before, but I can't seem to locate the mail or recall the answer! In the code below, how can I eliminate f()?
#include "boost/iterator/counting_iterator.hpp" #include "boost/bind.hpp" #include <iostream>
using namespace std; using namespace boost;
ostream & f( ostream & s, int i ) { return s << i; }
int main( ) { for_each( make_counting_iterator( 0 ), make_counting_iterator( 10 ), bind( & f, ref( cout ), _1 ) ); }
If I write something like
bind( static_cast< ostream & ( * )( ostream &, int ) >( operator << ), ref( cout ), _1 )
the compiler can't locate operator<<()
Any thoughts, solutions even?
operator<< for int a member function.
template<class charT, class traits> basic_ostream<charT,traits>& std::basic_ostream<charT,traits>::operator<<(int n);
Look at Phoenix's bind instead of the root bind. Phoenix's bind combines the root bind and lambda's bind all in one. something like this I think would work: // these are all phoenix's version, do not include boost bind, you do not even need bind... int main( ) { for_each( make_counting_iterator( 0 ), make_counting_iterator( 10 ), cout<<arg1 ); } I do not remember if cout need to be phoenixized or not, if that does not work then tell me and I will compile a quick file for a test to be sure. You might need to put the phoenix ref(cout) instead of just cout as well, do not recall off hand... Phoenix docs: http://spirit.sourceforge.net/dl_docs/phoenix-2/libs/spirit/phoenix/doc/html...