Boost: lambda expression not compiling

I tried to write a function that calculates the hamming distance between two codewords using the lambda boost library. I have the following code:

#include <iostream> #include <numeric> #include <boost/function.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/if.hpp> #include <boost/bind.hpp> #include <boost/array.hpp> template<typename Container> int hammingDistance(Container & a, Container & b) { return std::inner_product( a.begin(), a.end(), b.begin(), (_1 + _2), boost::lambda::if_then_else_return(_1 != _2, 1, 0) ); } int main() { boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1}; std::cout << hammingDistance(a, b) << std::endl; } 

And the error I get is:

 HammingDistance.cpp: In function 'int hammingDistance(Container&, Container&)': HammingDistance.cpp:15: error: no match for 'operator+' in '<unnamed>::_1 + <unnamed>::_2' HammingDistance.cpp:17: error: no match for 'operator!=' in '<unnamed>::_1 != <unnamed>::_2' /usr/include/c++/4.3/boost/function/function_base.hpp:757: note: candidates are: bool boost::operator!=(boost::detail::function::useless_clear_type*, const boost::function_base&) /usr/include/c++/4.3/boost/function/function_base.hpp:745: note: bool boost::operator!=(const boost::function_base&, boost::detail::function::useless_clear_type*) 

This is the first time I play with a reinforced lambda. Please tell me where I am wrong. Thanks.

EDIT:

Thanks a lot guys! Here is the working code (for reference only):

 #include <iostream> #include <numeric> #include <boost/function.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/if.hpp> #include <boost/lambda/bind.hpp> #include <boost/array.hpp> using boost::lambda::_1; using boost::lambda::_2; template<typename Container> int hammingDistance(Container & a, Container & b) { return std::inner_product( a.begin(), a.end(), b.begin(), 0, (_1 + _2), boost::lambda::if_then_else_return(_1 != _2, 1, 0) ); } int main() { boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1}; std::cout << hammingDistance(a, b) << std::endl; } 
+4
source share
2 answers

First problem: when using boost/lambda include <boost/lambda/bind.hpp> instead of <boost/bind.hpp>

Second problem: you will need using namespace boost::lambda after #includes

still not compiling though


Edit:
The third problem - you need 6 arguments for std::inner_product , you are missing an initialization argument. probably add 0 as the fourth argument.

+5
source

I could be wrong, but I think you should have using namespace boost::lambda; before your function is filled with placeholders (_1, _2, etc.) in this namespace.

+1
source

Source: https://habr.com/ru/post/1305743/


All Articles