尝试使用指针实例化std :: thread时出现令人困惑的clang错误

时间:2015-06-25 07:08:13

标签: c++11 clang++

在看Thread and interfaces C++时,我注意到我的Clang有些奇怪。

我有c++ --version

的输出
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix

编译以下内容

#include <thread>

class Foo {
public:
    void operator()() { }
};

int main() {
    Foo *foo = new Foo();
    std::thread t(foo);
    t.join();
    delete foo;
}
带有c++ thread.cpp

会产生以下明显错误:

In file included from thread.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:369:5: error: called object type 'Foo *' is not a function or
      function pointer
    (*__p)();
    ^~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:377:42: note: in instantiation of function template
      specialization 'std::__1::__thread_proxy<Foo *>' requested here
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
                                         ^
thread.cpp:10:17: note: in instantiation of function template specialization 'std::__1::thread::thread<Foo *>' requested here
    std::thread t(foo);
                ^
1 error generated.

总感 - Foo *不是函数指针。

但是,用c++ -std=c++11 thread.cpp编译它会产生这个神秘的错误:

In file included from thread.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:332:5: error: attempt to use a deleted function
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:342:5: note: in instantiation of function template
      specialization 'std::__1::__thread_execute<Foo *>' requested here
    __thread_execute(*__p, _Index());
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:354:42: note: in instantiation of function template
      specialization 'std::__1::__thread_proxy<std::__1::tuple<Foo *> >' requested here
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
                                         ^
thread.cpp:10:17: note: in instantiation of function template specialization 'std::__1::thread::thread<Foo *&, void>' requested here
    std::thread t(foo);
                ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:1027:5: note: '~__nat' has been explicitly marked
      deleted here
    ~__nat() = delete;
    ^
1 error generated.

导致这个关于已删除的析构函数的奇怪错误消息的原因是什么?我是否应该将Clang中的错误视为有这么奇怪的消息?

1 个答案:

答案 0 :(得分:0)

在两种情况下,您的实际错误消息是相同的:无论使用什么模板来实现std :: thread,它都不能专门用于Foo *。
~__nat() = delete;只是旧标准与新标准之间的随机差异,是由于类型错误导致失败的无趣之一。