
Hi there. I have a recent CVS version of Boost installed, and am using g++ v3.2. I tried to compile the following program: -----------lambda_test.cpp------------ #include <iostream> #include <string> #include <list> #include <algorithm> #include <boost/lambda/lambda.hpp> using namespace std; using namespace boost::lambda; int main() { list<int> ell; for (int i=0; i<100; ++i) ell.push_back(4*i-1); for_each(ell.begin(), ell.end(), cout << _1 << endl); } -------------------------------------- I am met by: % g++ -I/usr/local/boost -c lambda_test.cpp lambda_test.cpp: In function `int main()': lambda_test.cpp:17: no match for `const boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda:: bitwise_action<boost::lambda::leftshift_action>, boost::tuples::tuple<std::ostream&, boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > > << <unknown type>' operator Any suggestions? I am aware that I can replace endl by '\n' and get a compilable program. I worry that this problem extends to other manipulators... Thanks, Ken

Hi Ken,
for_each(ell.begin(), ell.end(), cout << _1 << endl);
Any suggestions? I am aware that I can replace endl by '\n' and get a compilable program. I worry that this problem extends to other manipulators...
It's not a bug, just an unfortunate restriction. This is an extract form Lambda wiki page: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Lambda Example: endl is not now allowed in std::cout statements in a Lambda expression. '\n' has to be used instead. -- This is because the old style streams were not templates and the new style ones are. Also, endl has been changed from a non-template function to a template function and one cannot take the address of a function template (withtout instantiated it at the same time. For example, in _1 << endl, the library does not know what the type of the stream that later is substituted for _1 will be. -- Jaakko Jarvi Jaakko

On Fri, 2002-09-13 at 12:22, Jaakko Jarvi wrote:
Hi Ken,
for_each(ell.begin(), ell.end(), cout << _1 << endl);
Any suggestions? I am aware that I can replace endl by '\n' and get a compilable program. I worry that this problem extends to other manipulators...
It's not a bug, just an unfortunate restriction.
Thanks for the quick response.
Example: endl is not now allowed in std::cout statements in a Lambda expression. '\n' has to be used instead.
-- This is because the old style streams were not templates and the new style ones are. Also, endl has been changed from a non-template function to a template function and one cannot take the address of a function template (withtout instantiated it at the same time. For example, in _1 << endl, the library does not know what the type of the stream that later is substituted for _1 will be. -- Jaakko Jarvi
So if I explicitly list the template parameters, does it work? Hmmm... No, this doesn't work: for_each(ell.begin(), ell.end(), cout << _1 << endl<char, char_traits<char> >); I was hopeful --- I'm pretty sure I have the syntax right. I'm simply curious now: is it possible to make this work? Ken -- Ken Yarnall Dept. of Mathematical Sciences Lebanon Valley College Assoc. Professor of Math and CS (717)867-6085 yarnall@lvc.edu

So if I explicitly list the template parameters, does it work? Hmmm...
No, this doesn't work:
for_each(ell.begin(), ell.end(), cout << _1 << endl<char, char_traits<char> >);
I was hopeful --- I'm pretty sure I have the syntax right. I'm simply curious now: is it possible to make this work?
Explicit specialization should work, just tested under gcc3.2. Here's another option, a helper function manip that takes a stream and a manipulator and chooses the appropriate instantiation of the endl template. Much nicer to just give the stream object rather than the stream template args: #include "boost/lambda/lambda.hpp" #include <iostream> namespace boost { namespace lambda { template <class CharT, class Traits> inline std::basic_ostream<CharT, Traits>& (*manip(std::basic_ostream<CharT, Traits> &, std::basic_ostream<CharT, Traits> &(*m) (std::basic_ostream<CharT, Traits> &))) (std::basic_ostream<CharT, Traits> &) { return m; } } } int main () { using namespace std; using namespace boost::lambda; (cout << _1 << manip(cout, endl) << "Work!\n")("Seems to"); return 0; } Cheers, Jaakko
Ken -- Ken Yarnall Dept. of Mathematical Sciences Lebanon Valley College Assoc. Professor of Math and CS (717)867-6085 yarnall@lvc.edu
Yahoo! Groups Sponsor ADVERTISEMENT [d_300x250_071_4for49_1.gif]
Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com>
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
-- -- -- Jaakko Järvi email: jajarvi@cs.indiana.edu -- Post Doctoral Fellow phone: +1 (812) 855-3608 -- Pervasive Technology Labs fax: +1 (812) 855-4829 -- Indiana University, Bloomington
participants (3)
-
Jaakko Jarvi
-
Ken Yarnall
-
Kenneth Yarnall