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

Discussion in 'C++' started by silverburgh.meryl@gmail.com, Feb 25, 2007.

  1. Guest

    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'
     
    , Feb 25, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Feb 26, 2007
    #2
    1. Advertising

  3. Piyo Guest

    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, Feb 26, 2007
    #3
  4. Piyo Guest

    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, Feb 26, 2007
    #4
  5. Piyo Guest

    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
     
    Piyo, Feb 26, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toby Bradshaw
    Replies:
    6
    Views:
    1,816
    Kai-Uwe Bux
    Jun 2, 2006
  2. Misiu
    Replies:
    3
    Views:
    2,453
    Misiu
    Jan 31, 2007
  3. Replies:
    0
    Views:
    614
  4. Barry Ding

    boost::lambda::bind

    Barry Ding, Jul 22, 2007, in forum: C++
    Replies:
    0
    Views:
    615
    Barry Ding
    Jul 22, 2007
  5. Christopher
    Replies:
    1
    Views:
    862
    Yakov Gerlovin
    Oct 5, 2011
Loading...

Share This Page