Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Compile error when use Boost::lambda::bind function (http://www.velocityreviews.com/forums/t460720-compile-error-when-use-boost-lambda-bind-function.html)

silverburgh.meryl@gmail.com 02-25-2007 11:31 PM

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'


Victor Bazarov 02-26-2007 03:31 AM

Re: Compile error when use Boost::lambda::bind function
 
silverburgh.meryl@gmail.com 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



Piyo 02-26-2007 03:32 AM

Re: Compile error when use Boost::lambda::bind function
 
silverburgh.meryl@gmail.com 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'
>


Piyo 02-26-2007 03:35 AM

Re: Compile error when use Boost::lambda::bind function
 
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);



Piyo 02-26-2007 04:26 AM

Re: Compile error when use Boost::lambda::bind function
 
silverburgh.meryl@gmail.com 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


All times are GMT. The time now is 06:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.