[lambda] if-then-else construct gets broken by lambda-lib

Hello everybody! I'm trying to store a reference to a function depending upon a condition within a function object like this: boost::function< double (std::size_t) > wf = (sIndex < m_maxIndex+1) ? ( boost::lambda::bind(boost::ref(m_cf), sDeg, boost::lambda::_1) ) : ( boost::ref(*m_wf) ); However, this does not compile. The error message is: error: no match for ternary 'operator?:' in '(sIndex < (((boost::cor_cm_iterator4<RandomGenerator, Graph>*)this)->boost::cor_cm_iterator4<RandomGenerator, Graph>::m_maxIndex + 1)) ? boost::lambda::bind(boost::ref(((boost::cor_cm_iterator4<RandomGenerator, Graph>*)this)->boost::cor_cm_iterator4<RandomGenerator, Graph>::m_cf), sDeg, boost::lambda::<unnamed>::_1) : boost::ref((*((boost::cor_cm_iterator4<RandomGenerator, Graph>*)this)->boost::cor_cm_iterator4<RandomGenerator, Graph>::m_wf))' But the lengthy code boost::function< double (std::size_t) > wf; if(sIndex < m_maxIndex+1) wf = boost::lambda::bind(boost::ref(m_cf), sDeg, boost::lambda::_1); else wf = boost::ref(*m_wf); compiles fine. Any idea? How can lambda eat up the ? : construct? How do I prevent that? My system is a ubunut edgy which means, I have g++ 4.1 and use boost 1.33.1. Greetings, Sebastian Weber

On 11/7/06, Sebastian Weber <sebastian.weber@physik.tu-darmstadt.de> wrote:
Hello everybody!
I'm trying to store a reference to a function depending upon a condition within a function object like this:
boost::function< double (std::size_t) > wf = (sIndex < m_maxIndex+1) ? ( boost::lambda::bind(boost::ref(m_cf), sDeg, boost::lambda::_1) ) : ( boost::ref(*m_wf) );
<snip>
Greetings,
Sebastian Weber
Try this - boost::function< double (std::size_t) > wf = (sIndex < m_maxIndex+1) ? boost::function< double (std::size_t) >( boost::lambda::bind(boost::ref(m_cf), sDeg, boost::lambda::_1) ) ) : ( boost::ref(*m_wf) ); i.e. make sure the first expression is explicitly a boost::function< double (std::size_t) >, as the second is convertible to that. Stuart Dootson

Hi Stuart!
Try this -
boost::function< double (std::size_t) > wf = (sIndex < m_maxIndex+1) ? boost::function< double (std::size_t) >( boost::lambda::bind(boost::ref(m_cf), sDeg, boost::lambda::_1) ) ) : ( boost::ref(*m_wf) );
i.e. make sure the first expression is explicitly a boost::function< double (std::size_t) >, as the second is convertible to that.
Cool, this works! But why is this explicit cast necessary in this case? Greetings, Sebastian Weber
Stuart Dootson _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Nov 8, 2006, at 1:42 AM, Sebastian Weber wrote:
Hi Stuart!
Try this -
boost::function< double (std::size_t) > wf = (sIndex < m_maxIndex +1) ? boost::function< double (std::size_t) >( boost::lambda::bind(boost::ref(m_cf), sDeg, boost::lambda::_1) ) ) : ( boost::ref(*m_wf) );
i.e. make sure the first expression is explicitly a boost::function< double (std::size_t) >, as the second is convertible to that.
Cool, this works! But why is this explicit cast necessary in this case?
It follows from the language rules of the ? : operator. Roughly, the branches have to have same types, or one convertible to another. The types of the expressions in the branches in your example look something like this: lambda_functor< some long template mess here > lambda_functor< some long template mess here, but different than the one in the other branch> These types are not convertible. Best, Jaakko Järvi
participants (3)
-
Jaakko Jarvi
-
Sebastian Weber
-
Stuart Dootson