Confusing clang error attempting to instantiate std::thread with a pointer

602 views Asked by At

While looking at Thread and interfaces C++, I noticed something a little strange with my Clang.

I have c++ --version output of

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

Compiling the following

#include <thread>

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

int main() {
    Foo *foo = new Foo();
    std::thread t(foo);
    t.join();
    delete foo;
}

with c++ thread.cpp yields the following sensible error:

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.

Total sense - Foo * isn't a function pointer.

But, compiling it with c++ -std=c++11 thread.cpp gives this cryptic error:

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.

What's causing this weird error message about a deleted destructor? Should I consider it a bug in Clang to have such an odd message?

1

There are 1 answers

0
Lorenzo Gatti On

Your actual error message is the same in the two cases: whatever template is used to implement std::thread, it cannot be specialized for Foo*.
~__nat() = delete; is only a random difference between the old and new standard, one of the uninteresting things that fail because of the type error.