problem with boost::any, boost::lambda

The following small code snippet does not compile with boost 1.33.1. What did I do wrong or the library is limited in what it can do? g++ -fstrict-aliasing -fomit-frame-pointer -Wall -pedantic -ansi -g -O2 anytest.cpp -o anytest anytest.cpp: In function ‘int main()’: anytest.cpp:22: error: ‘const struct boost::lambda::lambda_functor<boost::lambda::placeholder<1> >’ has no member named ‘type’ gcc version 4.1.1 20060525 (Red Hat 4.1.1-1) #include <iostream> #include <string> #include <vector> #include <algorithm> #include <boost/any.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/if.hpp> using namespace std; using namespace boost::lambda; using namespace boost; using boost::any_cast; int main(){ vector<any> a; any val = 3; a.push_back(val); val = string("456"); a.push_back(val); std::for_each(a.begin, a.end(), if_then(_1.type() == typeid(int))[cout << _1 << ' ']); }

Fei Liu wrote:
using namespace std; using namespace boost::lambda; using namespace boost; using boost::any_cast;
int main(){ vector<any> a;
std::for_each(a.begin, a.end(), if_then(_1.type() == typeid(int))[cout << _1 << ' ']); }
Invoking a method on a placeholder is a documented limitation: http://boost.org/doc/html/lambda/le_in_details.html#id1244744 The '.' operator cannot be overloaded. Therefore you must use bind(). Because you're using boost::lambda::if_then, I'm sure you're better off using boost::lambda::bind than plain boost::bind, though the fact that both have been brought into the current namespace makes me think that an explicit reference might help -- something like: boost::lambda::bind(&any::type, _1) (Disclaimer: I haven't tried to compile your example, with or without my suggestion.)

Nat Goodspeed wrote:
Fei Liu wrote:
using namespace std; using namespace boost::lambda; using namespace boost; using boost::any_cast;
int main(){ vector<any> a;
std::for_each(a.begin, a.end(), if_then(_1.type() == typeid(int))[cout << _1 << ' ']); }
Invoking a method on a placeholder is a documented limitation:
http://boost.org/doc/html/lambda/le_in_details.html#id1244744
The '.' operator cannot be overloaded. Therefore you must use bind(). Because you're using boost::lambda::if_then, I'm sure you're better off using boost::lambda::bind than plain boost::bind, though the fact that both have been brought into the current namespace makes me think that an explicit reference might help -- something like:
boost::lambda::bind(&any::type, _1)
(Disclaimer: I haven't tried to compile your example, with or without my suggestion.) ______________________________________
Thank you, unfortunately I am getting even more cryptic error messages now after I use explicit bind call: #include <iostream> #include <string> #include <vector> #include <algorithm> #include <boost/any.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/if.hpp> using namespace std; using namespace boost::lambda; using namespace boost; using boost::any_cast; int main(){ vector<any> a; any val = 3; a.push_back(val); val = string("456"); a.push_back(val); std::for_each(a.begin, a.end(), if_then(boost::lambda::bind(&any::type, _1) == typeid(int))[cout << _1 << ' ']); }
participants (2)
-
Fei Liu
-
Nat Goodspeed