boost.bind and g++ 2.95.3 woes

Hi there, I have a problem compiling the following code with g++ 2.95.3 and boost 1.28.0. ---%<--- #include <boost/bind.hpp> #include <boost/function/function0.hpp> struct A { A(boost::function0<void> const &) { } }; void foo(boost::function0<void> const &) {} void bar(unsigned int) {} int main(int argc, char *argv[]) { boost::bind(foo, boost::bind(bar, 0)); // ok A a1(boost::bind(bar, 0)); // ok A a2(boost::bind(foo, boost::bind(bar, 0))); // error return 0; } --->%--- The line marked with error gives me a whole flood of error messages. Is this a problem with my version of gcc? Or am I doing a stupid mistake? TIA, Markus PS: Here are the errors I get: /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp: In method `boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int>::function0<void>(void)': /opt/ginit/lib/boost-1.28.0/boost/bind.hpp:163: instantiated from `boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > >::operator ()<void, void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &), boost::_bi::list0>(boost::_bi::type<void>, void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &), boost::_bi::list0 &) const' /opt/ginit/lib/boost-1.28.0/boost/bind/bind_template.hpp:21: instantiated from `boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >::operator ()()' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:127: instantiated from `boost::detail::function::void_function_obj_invoker0<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >,void>::invoke(boost::detail::function::any_pointer)' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:437: instantiated from `boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int>::assign_to<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > > >(boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >, boost::detail::function::function_obj_tag)' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:388: instantiated from `boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int>::assign_to<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > > >(boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >)' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:274: instantiated from `boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int>::function0<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > > >(boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >)' test_bind.cpp:16: instantiated from here /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:271: parameter `f' declared void /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:274: `f' undeclared (first use this function) /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:274: (Each undeclared identifier is reported only once /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:274: for each function it appears in.) /opt/ginit/lib/boost-1.28.0/boost/bind.hpp: In method `void boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > >::operator ()<void, void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &), boost::_bi::list0>(boost::_bi::type<void>, void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &), boost::_bi::list0 &) const': /opt/ginit/lib/boost-1.28.0/boost/bind/bind_template.hpp:21: instantiated from `boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >::operator ()()' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:127: instantiated from `boost::detail::function::void_function_obj_invoker0<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >,void>::invoke(boost::detail::function::any_pointer)' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:437: instantiated from `boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int>::assign_to<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > > >(boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >, boost::detail::function::function_obj_tag)' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:388: instantiated from `boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int>::assign_to<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > > >(boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >)' /opt/ginit/lib/boost-1.28.0/boost/function/function_template.hpp:274: instantiated from `boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int>::function0<boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > > >(boost::_bi::bind_t<void,void (*)(const boost::function0<void,boost::empty_function_policy,boost::empty_function_mixin,int> &),boost::_bi::list1<boost::_bi::bind_t<void,void (*)(unsigned int),boost::_bi::list1<boost::_bi::value<int> > > > >)' test_bind.cpp:16: instantiated from here /opt/ginit/lib/boost-1.28.0/boost/bind.hpp:163: Internal compiler error in `emit_move_insn_1', at expr.c:2829 Please submit a full bug report. See <URL:http://www.gnu.org/software/gcc/bugs.html> for instructions.

I have a problem compiling the following code with g++ 2.95.3 and boost 1.28.0.
---%<--- #include <boost/bind.hpp> #include <boost/function/function0.hpp>
struct A { A(boost::function0<void> const &) { } };
void foo(boost::function0<void> const &) {} void bar(unsigned int) {}
int main(int argc, char *argv[]) { boost::bind(foo, boost::bind(bar, 0)); // ok A a1(boost::bind(bar, 0)); // ok A a2(boost::bind(foo, boost::bind(bar, 0))); // error return 0; } --->%---
The line marked with error gives me a whole flood of error messages. Is this a problem with my version of gcc? Or am I doing a stupid mistake?
A mistake, but not a stupid one. The boost::bind(bar, 0) subexpression is interpreted as a nested bind; that is, bind assumes that you want foo(bar(0)), but you actually want foo(bind(bar, 0)). You can "protect" the nested bind from being evaluated by using the helper function protect() defined in boost/bind/protect.hpp; see the last two paragraphs of http://www.boost.org/libs/bind/bind.html#nested_binds

Peter Dimov wrote:
[snip]
A mistake, but not a stupid one. The boost::bind(bar, 0) subexpression is interpreted as a nested bind; that is, bind assumes that you want foo(bar(0)), but you actually want foo(bind(bar, 0)). You can "protect" the nested bind from being evaluated by using the helper function protect() defined in boost/bind/protect.hpp; see the last two paragraphs of
Hah, yes of course! Thanks a lot, Peter! Markus
participants (3)
-
Markus Schöpflin
-
Peter Dimov
-
yg-boost-users@m.gmane.org