Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compile error when use Boost::lambda::bind function

Reply
Thread Tools

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

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      02-25-2007


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'

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Piyo
Guest
Posts: n/a
 
      02-26-2007
(E-Mail Removed) 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'
>

 
Reply With Quote
 
Piyo
Guest
Posts: n/a
 
      02-26-2007
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);


 
Reply With Quote
 
Piyo
Guest
Posts: n/a
 
      02-26-2007
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compile the following source code in VC6// I have error inVC++6 but compile ok in GCC fAnSKyer C++ 2 06-07-2009 07:57 AM
Compile Error: ldap.c:424: error: ‘LDAP_OP T_X_TLS_PROTOCOL’undeclared (first use in this function) Xeno Campanoli Ruby 2 06-03-2009 09:02 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
compile error with Boost Function object and templated function (Intel C++ compiler) Faheem Mitha C++ 2 08-26-2006 06:16 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments