Help with boost::lambda expression

Posted by Venkat Shivanandan on Stack Overflow See other posts from Stack Overflow or by Venkat Shivanandan
Published on 2010-04-01T09:04:11Z Indexed on 2010/04/01 9:13 UTC
Read the original article Hit count: 525

Filed under:
|

I tried to write a function that calculates a hamming distance between two codewords using the boost lambda 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 am getting 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 am playing with boost lambda. Please tell me where I am going wrong. Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost