2015-08-15 163 views
7

我正在使用带有最新TDM-Gcc标头和库的clang的最新快照版本。当编译这个(使用-std = C++ 11标志):这是不正确的使用std :: bind或编译器错误?

#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 ::绑定或这是一些奇怪的编译器错误?它似乎只使用TDM Gcc就可以很好地编译。

+0

所以我想它是一个叮咚的bug? – Fr0stBit

+0

我不会急于致电错误,速度很快。检查你的环境,我[相信](http://ideone.com/pTM1cN)可能有东西在那里。 –

+0

铿锵3.6.2对我来说很好。 – 5gon12eder

回答

4

这段代码没问题。

除非你在安装过程中做了一些奇怪或不支持的事情,否则你的工具链有一个bug。

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

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

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

+1

[已经有报道](https://llvm.org/bugs/show_bug.cgi?id=24372)。 它发生在我身上的只有mingw32头文件和Clang x86,但它在mingw64头文件和Clang x64中可以正常工作。 –