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

Howard Hinnant hhinnant at apple.com
Sun May 22 08:13:06 PDT 2011


On May 22, 2011, at 6:13 AM, Jonathan Sauer wrote:

> 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?

Nope, not this time!  You found a bug in libc++, thanks!  In [func.bind.bind]/p10/b1 I was detecting when Tid is a reference_wrapper<T>.  But when the bind functor is const, I add that cv-qualifier to the bound args, and was not making the proper substitution when Tid is const reference_wrapper<T>.

Checked in fix revision 131852.

Thanks for following through with this!

Howard




More information about the cfe-dev mailing list