Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this incorrect use of std::bind or a compiler bug?

I am using the latest snapshot build of clang with latest TDM-Gcc headers and libs. When compiling this (using -std=c++11 flag):

#include <functional>
#include <iostream>

class Foo
{
    public:
        void Bar(int x)
        {
            std::cout << x << std::endl;
        }
};

int main()
{
    Foo foo;
    auto f = std::bind(&Foo::Bar, &foo, 5);
    f();
    return 0;
}

I get these errors:

In file included from Test.cpp:1:
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1426:7: error: static_assert failed "Wrong number of arguments for pointer-to-member"
      static_assert(_Varargs::value
      ^             ~~~~~~~~~~~~~~~
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1440:7: note: in instantiation of template class 'std::_Bind_check_arity<void (Foo::*)(int) __attribute__((thiscall)), Foo *, int>' requested here
    : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
      ^
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1461:5: note: in instantiation of template class 'std::_Bind_helper<false, void (Foo::*)(int) __attribute__((thiscall)), Foo *, int>' requested here
    _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
    ^
Test.cpp:16:14: note: while substituting deduced template arguments into function template 'bind' [with _Func = void (Foo::*)(int) __attribute__((thiscall)), _BoundArgs = <Foo *, int>]
    auto f = std::bind(&Foo::Bar, &foo, 5);
             ^
Test.cpp:16:14: error: no matching function for call to 'bind'
    auto f = std::bind(&Foo::Bar, &foo, 5);
             ^~~~~~~~~
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1490:5: note: candidate template ignored: couldn't infer template argument '_Result'
    bind(_Func&& __f, _BoundArgs&&... __args)
    ^
C:\DevEnv\LLVM38\lib\gcc\mingw32\5.1.0\include\c++\functional:1462:5: note: candidate template ignored: substitution failure [with _Func = void (Foo::*)(int) __attribute__((thiscall)), _BoundArgs = <Foo *, int>]
    bind(_Func&& __f, _BoundArgs&&... __args)
    ^
2 errors generated.

Am i misusing std::bind or is this some weird compiler error? It seems to compile fine using just TDM Gcc.

like image 437
Fr0stBit Avatar asked Aug 15 '15 22:08

Fr0stBit


People also ask

What is std :: bind used for?

std::bind. std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.

Is std :: bind deprecated?

Yes: std::bind should be replaced by lambda For almost all cases, std::bind should be replaced by a lambda expression. It's idiomatic, and results in better code.

What does boost:: bind do?

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.

What is_ 1 in boost:: bind?

_1 is a placeholder. Boost. Bind defines placeholders from _1 to _9 . These placeholders tell boost::bind() to return a function object that expects as many parameters as the placeholder with the greatest number.


1 Answers

This code is fine.

Unless you've done something weird or unsupported with your installation, your toolchain has a bug.

To me it looks like it's not considering the second argument to std::bind to be the "this pointer", and thinks you're actually passing two arguments to Foo::Bar (which you're not), which you can't.

The next step is to raise this with the maintainers of your toolchain.

See https://llvm.org/bugs/show_bug.cgi?id=24372.

like image 200
Lightness Races in Orbit Avatar answered Oct 26 '22 15:10

Lightness Races in Orbit