Boost logo

Boost :

Subject: [boost] [bind/lambda] Matching signature constness?
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2008-12-10 07:55:25


Here's a bit of code (and my apologies for it's length, but I'm unsure
which bit is the failure point)

#include <cstddef>

#include "boost/lambda/lambda.hpp"
#include "boost/lambda/bind.hpp"
#include "boost/function.hpp"
#include "boost/type_traits.hpp"

namespace
{
  template < bool >
    struct cmp_detail
  {
    template < typename T >
      static bool cmp( const T & lhs, const T & rhs )
    {
      return lhs != rhs;
    }
  };

  template < >
    struct cmp_detail< true >
  {
    template < typename T, std :: size_t sz >
      static bool cmp( const T ( & lhs )[ sz ], const T ( & rhs )[ sz ] )
    {
      using boost :: lambda :: _1;
      using boost :: lambda :: bind;
      using boost :: lambda :: var;

      boost :: function< bool( unsigned ) > cmp_element =
        bind( cmp_detail< boost :: is_array< T > :: value > :: cmp,
            var( lhs )[ _1 ], var( rhs )[ _1 ] );

      for ( unsigned i = 0; i != sz; ++ i )
        if ( cmp_element( i ) )
        // if ( cmp_detail< boost :: is_array< T > :: value > :: cmp( lhs[ i
], rhs[ i ] ) )
          return true;
      return false;
    }
  };

  template < typename T >
    bool cmp( const T & lhs, const T & rhs )
  {
    return cmp_detail< boost :: is_array< T > :: value > :: cmp( lhs, rhs );
  }

  template < typename T, std :: size_t sz >
    struct Block
  {
    typedef T type[ sz ][ sz ];
  };
}

int main( )
{
  Block< int, 16 > :: type a;
  Block< int, 16 > :: type b;

  cmp( a, b );
}

The problem is the bind call in cmp_detail<true>::cmp(...). Error is this

block.cpp: In static member function `static bool <unnamed>::cmp_detail<
true>::cmp(const T (&)[sz], const T (&)[sz]) [w
ith T = int[16], unsigned int sz = 16u]':
block.cpp:45: instantiated from `bool <unnamed>::cmp(const T&, const T&)
[with T = int[16][16]]'
block.cpp:61: instantiated from here
block.cpp:32: error: no matching function for call to `bind(<unknown type>,
const boost::lambda::lambda_functor<boost::l
ambda::lambda_functor_base<boost::lambda::other_action<boost::lambda::subscript_action>,
boost::tuples::tuple<boost::lam
bda::lambda_functor<boost::lambda::identity<const int (&)[16][16]> >,
boost::lambda::lambda_functor<boost::lambda::place
holder<1> >, boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type, boo
st::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type> > >, const boost::l
ambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::other_action<boost::lambda::subscript_action>,
b
oost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::identity<const
int (&)[16][16]> >, boost::lambda::lambd
a_functor<boost::lambda::placeholder<1> >, 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> > >)'

I think perhaps the var() calls are not preserving constness quite right?
Any help would be welcome.

Thanks.

- Rob.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk