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

标签 c++ c++11 clang mingw

我正在使用带有最新 TDM-Gcc header 和库的 clang 的最新快照构建。编译时(使用 -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 就可以很好地编译。

最佳答案

这段代码没问题。

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

对我来说,它似乎没有将 std::bind 的第二个参数视为“this pointer”,并认为你实际上将两个参数传递给 Foo::Bar(你不是),你不能。

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

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

关于c++ - 这是对 std::bind 的错误使用还是编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32030141/

相关文章:

C++ 如何在 C++ 中使用 dlopen()?

c++ - 无法为我的 LruCache 类定义模板化类型

c++ - UML 设计模式和 C++ 类的实现

c++ - 线程池怎么了?

c++ - 修复第三方代码 : "error: ‘enable_if’ in namespace ‘std’ does not name a template type"

C++ 类型索引散列导致未定义的行为

c++ - 使用 init_seg 调用初始化函数

c++ - 没有返回语句时的 Clang 优化 : if(false) is entered?

c++ - 在 Clang AST 中存储 Decl 节点

c++ - fork 、管道和文件操作