[cfe-dev] Compile error with libc++'s bind

Jonathan Sauer jonathan.sauer at gmx.de
Sun May 22 03:13:59 PDT 2011


Hello,

> I've submitted this bugzilla:
> 
> http://llvm.org/bugs/show_bug.cgi?id=9975

Well, that wasn't the first PR that was the result of my stupidity :-/ Thank you, Richard, for pointing
out the (obvious) mistake in my code! And thank you, Howard, for your patience!

But I still have my original problem with std::bind, the one that lead me to post the incorrect code here.
I managed to reduce it to the following:

#include <functional>

struct Container {
    void lookup() const;                // 1
};

void lookup(Container&);                // 2


template <typename Functor>
static void foo(const Functor& f)       // A
//static void foo(Functor f)            // B
{
    f();
}


int main(int, char**)
{
    Container   c;
    foo(std::bind(&Container::lookup, std::ref(c)));    // C
    //foo(std::bind(&Container::lookup, c));            // D
    //foo(std::bind(&lookup, std::ref(c)));             // E
}


This fails to compile with the combination (A)+(C) (functor is passed as a const reference, and std::ref is used
to avoid copying <c> into the functor), but compiles with all other combinations (well, modulo PR9983). It seems
that for some reason using std::ref requires the functor to be non-const, at least when a member function is bound
(the code compiles with the free function "lookup" (1), but not the member function "lookup" (2)).

(My clang and libc++ are both the current trunk.)

Am I (again) overlooking something?


Thanks in advance,
Jonathan





More information about the cfe-dev mailing list