[lambda][optional] Accessing an optional variable through lambda
I'm trying to create a stream operator for a list of DataPoint objects. I can't get the correct boost::lambda syntax to access what the optional is containing. Could someone tell me what I'm doing wrong? Ryan struct DataPoint { boost::optional<double> m_Time; }; std::ostream & operator<<(std::ostream & stream, std::list<DataPoint> const& points) { namespace bll = boost::lambda; std::for_each( points.begin(), points.end(), stream << bll::bind(&boost::optional<double>::get, bll::bind(&DataPoint::m_Time, bll::_1)) << bll::constant(" ")); //Would prefer to do something like this "*bll::bind(&DataPoint::m_Time, bll::_1)". This gives me quite the compile //error. Is there a way to use indirection or do I need to bind to a optional method to get access? return stream; }
Hi Ryan,
On Wed, Aug 11, 2010 at 8:05 PM, Ryan McConnehey
I'm trying to create a stream operator for a list of DataPoint objects. I can't get the correct boost::lambda syntax to access what the optional is containing. Could someone tell me what I'm doing wrong?
The problem appears to be that boost::optional<double>::get is
overloaded -- there's a const and a non-const version. The most
straightforward solution appears to be a cast:
#include
Nathan Crookston wrote:
To do what you'd like with Boost.Phoenix (#include
to get all of it) you could replace most of operator<< with the following: namespace bp = boost::phoenix; std::for_each(points.begin(), points.end(), stream << *bp::bind(&DataPoint::m_Time, bp::arg_names::_1) << " ");
Much cleaner, if that option's available to you.
Good luck,
Nate
Thanks for the alternative. It does look much cleaner. I've seen boost Phoenix around but haven't really gotten into to it. Is there a web article that describes the pros and cons of using Phoenix versus Lambda. From an outside perspective it looks like they do the same things. This of course leads me to ask why there are two libraries. Ryan
On Thu, Aug 12, 2010 at 1:47 AM, Ryan McConnehey
Thanks for the alternative. It does look much cleaner. I've seen boost Phoenix around but haven't really gotten into to it. Is there a web article that describes the pros and cons of using Phoenix versus Lambda. From an outside perspective it looks like they do the same things. This of course leads me to ask why there are two libraries.
If I understand correctly, Boost.Phoenix is intended to replace Boost.Lambda. In my experience the syntax is a little nicer and it has been easier to use & extend than lambda. The following thread provides a little more information: http://old.nabble.com/Phoenix3-port-to-proto-complete-td29247472.html Nate
Nathan Crookston wrote:
If I understand correctly, Boost.Phoenix is intended to replace Boost.Lambda. In my experience the syntax is a little nicer and it has been easier to use & extend than lambda.
The following thread provides a little more information: http://old.nabble.com/Phoenix3-port-to-proto-complete-td29247472.html
Thanks for the thread. It helped clear up my confusion. Is there any plan on pulling Phoenix out of the Spirit area into it own library in boost? Ryan
On Thu, Aug 12, 2010 at 12:24 PM, Ryan McConnehey
Thanks for the thread. It helped clear up my confusion. Is there any plan on pulling Phoenix out of the Spirit area into it own library in boost?
Yes. It was reviewed several months ago and conditionally accepted. It will be subjected to a mini-review prior to becoming a top-level boost library. I'm not sure when that will happen. Nate
participants (2)
-
Nathan Crookston
-
Ryan McConnehey