【问题标题】:Is this incorrect use of std::bind or a compiler bug?这是对 std::bind 的错误使用还是编译器错误?
【发布时间】:2015-08-15 22:50:02
【问题描述】:

我正在使用最新的 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:\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.

我是在滥用 std::bind 还是这是一些奇怪的编译器错误?它似乎只使用 TDM Gcc 编译得很好。

【问题讨论】:

  • 所以我想它是一个clang错误?
  • 我不会急于这么快地调用错误。检查你的环境,我believe那里可能有什么东西。
  • 好吧,我在 Windows 上遇到了这个问题..
  • 嗯,clang 对 libstdc++ 的 windows 实现甚至还没有完成......
  • 即使使用 GCC 标头的 Clang 也适用于我,以防这还不清楚。

标签: c++ c++11 clang mingw


【解决方案1】:

这段代码没问题。

除非您在安装过程中做了一些奇怪或不受支持的事情,否则您的工具链存在错误。

在我看来,它似乎没有将 std::bind 的第二个参数视为“this 指针”,并认为您实际上是在将两个参数传递给 Foo::Bar(您不是),您可以不。

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

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

【讨论】:

  • It already was reported。它只发生在 mingw32 头文件和 Clang x86 上,但它适用于 mingw64 头文件和 Clang x64。
猜你喜欢
  • 2016-11-13
  • 2012-04-08
  • 2011-08-03
  • 1970-01-01
  • 2012-03-18
  • 2018-10-04
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多