I'm taking a look at the Lambda library, and writing some toy programs, and
I keep coming up with errors, and I can't figure out what the problems
are....
Example 1:
This works as expected:
using namespace boost::lambda;
class Foo {
public:
void print() {
std::cout << "Foo::print()\n";
}
};
template <class Container, class Function>
Function for_all(Container cont, Function f) {
return std::for_each(cont.begin(), cont.end(), f);
}
int main(int argc, char* argv[]) {
std::vector<Foo> vec;
//Fill vec
for_all(vec, (bind(&Foo::print, _1)));
return 0;
}
If I change the declaration of for_all to take the container by const
reference, I get errors [See Error 1 pasted in below]:
using namespace boost::lambda;
class Foo {
public:
void print() {
std::cout << "Foo::print()\n";
}
};
template <class Container, class Function>
Function for_all(Container const &cont, Function f) {
return std::for_each(cont.begin(), cont.end(), f);
}
int main(int argc, char* argv[]) {
std::vector<Foo> vec;
//Fill vec
for_all(vec, (bind(&Foo::print, _1)));
return 0;
}
Example 2:
It looks from the docs like I should be able to rewrite the bind expression
above using operator ->*, like so:
for_all(vec, (&_1 ->* &Foo::print));
And, in fact this compiles cleanly (regardless of whether the container is
passed by value or by const reference). Foo::print() never gets called,
though. What am I doing wrong or not understanding here?
Thanks in advance!!!
--------------------------------
John Wismar
john.wismar(a)autozone.com
-------------------------------------------------------------------------------------------------------------
[Error 1]
c:\dev\alldata\Dev\3rd Party\boost\boost\lambda\detail\actions.hpp(87) :
error C2665: 'boost::lambda::function_adaptor<Func>::apply' : none of the 2
overloads can convert parameter 2 from type 'const
std::allocator<_Ty>::value_type'
with
[
Func=boost::lambda::return_type_N<boost::lambda::function_action<2>,boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>::sig<boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>>::rets_t>::plain_Func
]
and
[
_Ty=Foo
]
c:\dev\alldata\Dev\3rd
Party\boost\boost\lambda\detail\function_adaptors.hpp(193): could be
'Result boost::lambda::function_adaptor<Func>::apply<RET>(Result
(__thiscall Foo::* )(void),Object *)'
with
[
Result=void,
Func=boost::lambda::return_type_N<boost::lambda::function_action<2>,boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>::sig<boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>>::rets_t>::plain_Func
,
RET=boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boo
st::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>::sig<boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>>::type,
Object=Foo
]
c:\dev\alldata\Dev\3rd
Party\boost\boost\lambda\detail\function_adaptors.hpp(197): or
'Result boost::lambda::function_adaptor<Func>::apply<RET>(Result
(__thiscall Foo::* )(void),Object &)'
with
[
Result=void,
Func=boost::lambda::return_type_N<boost::lambda::function_action<2>,boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>::sig<boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>>::rets_t>::plain_Func
,
RET=boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boo
st::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>::sig<boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>>::type,
Object=Foo
]
while trying to match the argument list '(const rt0 , const
std::allocator<_Ty>::value_type)'
with
[
_Ty=Foo
]
c:\dev\alldata\Dev\3rd
Party\boost\boost\lambda\detail\lambda_functor_base.hpp(408) : see
reference to function template instantiation 'RET
boost::lambda::function_action<I>::apply<RET,U,const Foo>(A1 & ,A2 &)'
being compiled
with
[
RET=boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>::sig<boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>>::type,
I=2,
U=const rt0,
A1=const rt0,
A2=const std::allocator<Foo>::value_type
]
c:\dev\alldata\Dev\3rd
Party\boost\boost\lambda\detail\lambda_functors.hpp(148) : see reference to
function template instantiation 'RET
boost::lambda::lambda_functor_base<Act,Args>::call<boost::lambda::lambda_functor_base<Act,Args>::sig<SigArgs>::type,A,const
boost::tuples::null_type,const boost::tuples::null_type,const
boost::tuples::null_type>(A &,B &,C &,Env &) const' being compiled
with
[
RET=boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>::sig<boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>>::type,
Act=boost::lambda::action<2,boost::lambda::function_action<2>>,
Args=boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::typ
e,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>,
SigArgs=boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>,
A=const std::allocator<Foo>::value_type,
B=const boost::tuples::null_type,
C=const boost::tuples::null_type,
Env=const boost::tuples::null_type
]
c:\DevTools\VS.NET2003\Vc7\include\algorithm(21) : see reference to
function template instantiation
'boost::lambda::lambda_functor_base<Act,Args>::sig<SigArgs>::type
boost::lambda::lambda_functor<T>::operator ()<const
std::allocator<_Ty>::value_type>(A &) const' being compiled
with
[
Act=boost::lambda::action<2,boost::lambda::function_action<2>>,
Args=boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>,
SigArgs=boost::tuples::tuple<const
std::allocator<Foo>::value_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,boost::tuples::null_type,boost::tuples::null_type>,
T=boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<b
oost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>,
_Ty=Foo,
A=const std::allocator<Foo>::value_type
]
c:\Temp\LambdaTest\LambdaTest.cpp(29) : see reference to function
template instantiation '_Fn1
std::for_each<std::vector<_Ty>::const_iterator,Function>(_InIt,_InIt,_Fn1)'
being compiled
with
[
_Fn1=boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>>,
_Ty=Foo,
Function=boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>>,
_InIt=std::vector<Foo>::const_iterator
]
c:\Temp\LambdaTest\LambdaTest.cpp(35) : see reference to function
template instantiation 'Function
for_all<std::vector<_Ty>,boost::lambda::lambda_functor<T>>(const Container
&,Function)' being compiled
with
[
Function=boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>>,
_Ty=Foo,
T=boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2>>,boost::tuples::tuple<const
boost::lambda::detail::bind_traits<void (__thiscall Foo::* const
)(void)>::type,const boost::lambda::detail::bind_traits<const
boost::lambda::placeholder1_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>,
Container=std::vector<Foo>
]