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



 DEVELOP > c-Plus-Plus > Compile error when use Boost::lambda::bind function

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 25 Feb 2007 05:31:23 PM
Object: Compile error when use Boost::lambda::bind function
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'
.

User: "Piyo"

Title: Re: Compile error when use Boost::lambda::bind function 25 Feb 2007 09:32:17 PM
wrote:


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'

.

User: "Piyo"

Title: Re: Compile error when use Boost::lambda::bind function 25 Feb 2007 10:26:32 PM
wrote:


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
.

User: "Victor Bazarov"

Title: Re: Compile error when use Boost::lambda::bind function 25 Feb 2007 09:31:08 PM
wrote:

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'

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "Piyo"

Title: Re: Compile error when use Boost::lambda::bind function 25 Feb 2007 09:35:28 PM
Victor Bazarov wrote:

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);
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER