这是对std::bind的错误使用还是编译器错误?

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

本文关键字:错误 编译器 std bind      更新时间:2023-10-16

我正在使用最新的快照构建的clang与最新的TDM-Gcc头文件和库。编译时(使用-std=c++11标志):

#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;
}

我得到这些错误:

In file included from Test.cpp:1:
C:DevEnvLLVM38libgccmingw325.1.0includec++functional:1426:7: error: static_assert failed "Wrong number of arguments for pointer-to-member"
      static_assert(_Varargs::value
      ^             ~~~~~~~~~~~~~~~
C:DevEnvLLVM38libgccmingw325.1.0includec++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:DevEnvLLVM38libgccmingw325.1.0includec++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:DevEnvLLVM38libgccmingw325.1.0includec++functional:1490:5: note: candidate template ignored: couldn't infer template argument '_Result'
    bind(_Func&& __f, _BoundArgs&&... __args)
    ^
C:DevEnvLLVM38libgccmingw325.1.0includec++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.

是我滥用std::bind还是这是一些奇怪的编译错误?使用TDM Gcc似乎可以很好地编译。

这段代码很好。

除非你做了一些奇怪的事情或者你的安装不支持,否则你的工具链有一个错误。

对我来说,它看起来不考虑std::bind的第二个参数是"这个指针",并认为你实际上传递了两个参数给Foo::Bar(你不是),这是你不能的。

下一步是向工具链的维护者提出这个问题。

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