3

I'm trying to use std::thread. My thread is supposed to call a method and pass a struct as a parameter, as so many examples show. Except my very simple code won't compile. For the record, I'm aware of this question but nothing there seems to help me.

Where I call the thread:

void Exporter::save() const {
    thread(write_to_disk, this->parameter).detach();
}

The signature of write_to_disk:

void write_to_disk(const Parameter& parameter)

write_to_disk is defined in a nameless namespace in the .cpp file.

I get the following error:

src/Exporter.cpp:65:5: error: no matching constructor for initialization of 'std::__1::thread'
    thread(write_to_disk, this->parameter).detach();
    ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:374:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments were provided
thread::thread(_Fp __f)
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:263:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    thread(const thread&);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:270:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(0) {}
^
8
  • 1
    Here's an MCVE that fits the description in the question and compiles and runs just fine. Can you provide an MCVE that doesn't? Commented Dec 22, 2014 at 16:56
  • This is the closest I can come to my code, but it runs perfectly. I also tried passing a simple int to write_to_disk and it fails with the same error. I wonder if it's a platform issue... Commented Dec 22, 2014 at 17:03
  • Also, not sure I trust this online compiler. There was a syntax error and it worked fine. Commented Dec 22, 2014 at 17:04
  • Which version of Xcode/clang are you using? A simple test on this end of something quite similar works fine for me. Commented Dec 22, 2014 at 17:05
  • Latest Xcode: $ clang --version Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.0.0 Thread model: posix Commented Dec 22, 2014 at 17:07

1 Answer 1

6

Works fine for me if I do

clang++ -std=c++11 test.cpp
Sign up to request clarification or add additional context in comments.

1 Comment

clang++ -std=c++11 fixed it. I suspect CMake is messing up my ninja files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.