[lambda / multi_index] compilation problem

Hi, getting lambda work with multi_index seems a bit tough for me.... the following compiles fine with MSVC++ 2003, but not when I #include sequenced_index.hpp or ordered_index.hpp. The error message says: error C2872: '_1' : ambiguous symbol could be 'c:\Boost\include\boost-1_33_1\boost\lambda\core.hpp(69) : const boost::lambda::placeholder1_type &boost::lambda::`anonymous-namespace'::_1' or 'c:\Boost\include\boost-1_33_1\boost\bind\placeholders.hpp(42) : boost::arg<I> `anonymous-namespace'::_1' with [ I=1 ] This can of course be fixed by using boost::lambda::_1, but I doubt that it is the expected behavior. Can anyone please explain what I'm doing wrong? Thanks, Filip #include <boost/config.hpp> #include <boost/lambda/lambda.hpp> #include <boost/multi_index_container.hpp> #include <boost/multi_index/member.hpp> #include <boost/multi_index/hashed_index.hpp> //#include <boost/multi_index/sequenced_index.hpp> //#include <boost/multi_index/ordered_index.hpp> #include <list> using namespace boost; using namespace boost::lambda; int main(int argc, char* argv[]) { std::list<int> v(10); for_each(v.begin(), v.end(), _1 = 1); return 0; }

Filip Konvi?ka ha escrito:
Hi,
getting lambda work with multi_index seems a bit tough for me.... the following compiles fine with MSVC++ 2003, but not when I #include sequenced_index.hpp or ordered_index.hpp.
The error message says: error C2872: '_1' : ambiguous symbol could be 'c:\Boost\include\boost-1_33_1\boost\lambda\core.hpp(69) : const boost::lambda::placeholder1_type &boost::lambda::`anonymous-namespace'::_1' or 'c:\Boost\include\boost-1_33_1\boost\bind\placeholders.hpp(42) : boost::arg<I> `anonymous-namespace'::_1' with [ I=1 ]
Hello Filip, the problem you describe is only incidentally related with Boost.MultiIndex (anyway, thank you for using the lib!): actually, it's a manifestation of a symbol clash between Boost.Lambda and Boost.Bind: #include <boost/config.hpp> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> #include <list> using namespace boost; using namespace boost::lambda; int main(int argc, char* argv[]) { std::list<int> v(10); for_each(v.begin(), v.end(), _1 = 1); return 0; } You're having the error under the conditions you describe just because boost/multi_index/sequenced_index.hpp and boost/multi_index/ordered_index.hpp internally include boost/bind.hpp.
This can of course be fixed by using boost::lambda::_1, but I doubt that it is the expected behavior.
I guess the using declaration is a perfectly good solution if you don't use Boost.Bind in the offending translation unit: This is a well known problem with Boost.Bind defining its placeholders in an unnamed space (::_1,::_2,...), which causes ambiguity problems when combined with the "using namespace boost::lambda" directive. You might want to read the following thread for further info on the issue: http://lists.boost.org/Archives/boost/2004/11/76822.php AFAIK this is not currently being addressed in any way.
Can anyone please explain what I'm doing wrong?
HTH, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

Joaquín Mª López Muñoz wrote on 13.3.2007 14:55:
Hello Filip, the problem you describe is only incidentally related with Boost.MultiIndex (anyway, thank you for using the lib!): I thank you! It's just wonderful. I'm coming back to C++ after several years and I'm excited about the new stuff. actually, it's a manifestation of a symbol clash between Boost.Lambda and Boost.Bind:
...
You're having the error under the conditions you describe just because boost/multi_index/sequenced_index.hpp and boost/multi_index/ordered_index.hpp internally include boost/bind.hpp.
This can of course be fixed by using boost::lambda::_1, but I doubt that it is the expected behavior. I guess the using declaration is a perfectly good solution if you don't use Boost.Bind in the offending translation unit: This is a well known problem with Boost.Bind defining its placeholders in an unnamed space (::_1,::_2,...), which causes ambiguity problems when combined with the "using namespace boost::lambda" directive. You might want to read the following thread for further info on the issue:
http://lists.boost.org/Archives/boost/2004/11/76822.php
AFAIK this is not currently being addressed in any way.
Hm, it's a pity that a global "using boost::lambda::_1;" doesn't work. So I'll probably stick with "lambda::_1" in simple cases, and with local "using lambda::_1;" in more complex ones. Thanks for the valuable info! Cheers, Filip
participants (2)
-
Filip Konvička
-
Joaquín Mª López Muñoz