Compile error when use Boost::lambda::bind function

S

silverburgh.meryl

I have code which uses Boost lambda in a template like this:
using namespace boost::lambda;

template<class T>
bool lessThanXY( T& src, T& dest ) {

return (src.getY() < dest.getY());
}

template<class T1, class T2>
void sortXY(T1& list) {
::sort( list.begin(), list.end(), bind:):lessThanXY<T2>, *(_1),
*(_2) ) );
}

But when I instantiate the template like this, I get the following
compile error:

class Rect;
typedef list<Rect*> RectList;

void sortXYRectList(RectList& rl) {
sortXYWithThreshold<RectList, Rect>(rl);
}


/usr/include/boost/lambda/detail/lambda_traits.hpp:389: error:
ignoring 'const' qualifiers added to function type 'bool ()(Rect&,
Rect&)const'
gmake[5]: *** [RectUtils.o] Error 1

Can you please tell me how to fix my problem ? I did not use 'const'
anywhere in my template, why I get this error:

error: ignoring 'const' qualifiers added to function type 'bool
()(Rect&, Rect&)const'
 
V

Victor Bazarov

I have code which uses Boost lambda in a template like this:
using namespace boost::lambda;

template<class T>
bool lessThanXY( T& src, T& dest ) {

return (src.getY() < dest.getY());
}

*(_2) ) );
}

But when I instantiate the template like this, I get the following
compile error:

class Rect;
typedef list<Rect*> RectList;

void sortXYRectList(RectList& rl) {
sortXYWithThreshold<RectList, Rect>(rl);
}


/usr/include/boost/lambda/detail/lambda_traits.hpp:389: error:
ignoring 'const' qualifiers added to function type 'bool ()(Rect&,
Rect&)const'
gmake[5]: *** [RectUtils.o] Error 1

Can you please tell me how to fix my problem ? I did not use 'const'
anywhere in my template, why I get this error:

error: ignoring 'const' qualifiers added to function type 'bool
()(Rect&, Rect&)const'

I don't know the contents (implementation) of 'lambda_traits', but
apparently since you _did not_ use 'const' with the arguments of
your lessThanXY function, it has to drop its own 'const' it probably
applied somewhere on the road to 'lessThanXY'. And why _didn't_ you
use 'const'? Your 'lessThanXY' doesn't attempt to change its args,
does it? So its signature should be "bool (T const&, T const&)".
Try it.

V
 
P

Piyo

I have code which uses Boost lambda in a template like this:
using namespace boost::lambda;

template<class T>
bool lessThanXY( T& src, T& dest ) {

return (src.getY() < dest.getY());
}


I am guessing here but bind() produces a function object
that looks like this (I removed templates for brevity)

class bindFunctionObject
{
public:
bool operator()( Rect &, Rect &) const;
};

Notice that the function object generated by bind has a function
call operator that is const since the operator does not modify
(or need not modify) any internal state of the function object.
That is where the const is coming from. But I am not sure as to
why boost::lamba would hate it.

Also, are you sure that you are supposed to use ::sort()? Sort that
has that signature is found in <algorithm> and is

std::sort(). Maybe there is a confusion somewhere because of this?

It might help if you post a short but complete version of your
program that demonstrates this issue.

HTH
template<class T1, class T2>
void sortXY(T1& list) {
::sort( list.begin(), list.end(), bind:):lessThanXY<T2>, *(_1),
*(_2) ) );
}

But when I instantiate the template like this, I get the following
compile error:

class Rect;
typedef list<Rect*> RectList;

void sortXYRectList(RectList& rl) {
sortXYWithThreshold<RectList, Rect>(rl);
}


/usr/include/boost/lambda/detail/lambda_traits.hpp:389: error:
ignoring 'const' qualifiers added to function type 'bool ()(Rect&,
Rect&)const'
gmake[5]: *** [RectUtils.o] Error 1

Can you please tell me how to fix my problem ? I did not use 'const'
anywhere in my template, why I get this error:

error: ignoring 'const' qualifiers added to function type 'bool
()(Rect&, Rect&)const'
 
P

Piyo

Victor said:
I don't know the contents (implementation) of 'lambda_traits', but
apparently since you _did not_ use 'const' with the arguments of
your lessThanXY function, it has to drop its own 'const' it probably
applied somewhere on the road to 'lessThanXY'. And why _didn't_ you
use 'const'? Your 'lessThanXY' doesn't attempt to change its args,
does it? So its signature should be "bool (T const&, T const&)".
Try it.

V

Oh yeah, that's correct! I missed that. I just checked the headers for
std::less<> and the sig for the function call operator reads:

bool operator()(const T& x, const T& y);
 
P

Piyo

template<class T1, class T2>
void sortXY(T1& list) {
::sort( list.begin(), list.end(), bind:):lessThanXY<T2>, *(_1),
*(_2) ) );
}

BTW, since you use boost, you can use the magic of type_traits
to make your life easier by reducing the number of template
parameters.

#include <boost/type_traits/remove_pointer.hpp>

template< typename T >
void sortXY( const T &list )
{
// the lines got too long so I separated it
// but you can put this entire typedef in as the
// template paramter of your lessThanXY.
// by doing so, you can even eliminate the sortXY
// function altogether if you wanted to.
using namespace boost;
typedef typename
remove_pointer<typename T::value_type>::type val_type;

std::sort( list.begin(), list.end(),
bind:):lessThanXY<val_type>, *(_1), *(_2) ));
}

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top