C++ 为什么可以';我不能编译这个简单的线程测试吗?

C++ 为什么可以';我不能编译这个简单的线程测试吗?,c++,multithreading,macos,C++,Multithreading,Macos,我想在我的MacBookPro上用线程测试一些东西,但我无法让它工作 Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1 Apple LLVM version 10.0.1 (clang-1001.0.46.4) T

我想在我的MacBookPro上用线程测试一些东西,但我无法让它工作

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
这是安装在我的机器上的clang版本。我试图编写一些线程向量,但没有成功,所以我返回并从so复制了一个示例

#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Do other things...

    // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}

我想是我的机器出了问题,但为什么呢?

不知为什么,您将代码构建为C++03,可能是因为没有明确提供标准修订标志。libc++,标准库的LLVM实现允许在C++03代码中使用
。源具有以下内容的条件编译:


参数“Hello”不是一个string@SeverinPappadeux-没关系,衰减的指针可用于初始化string@HWilmer,是否在编译器调用中添加显式的
-std=c++11
(或更高)标志?请显示生成命令。发布的代码与苹果LLVM版本10.0.1(clang-1001.0.46.4)一起工作,在我的装备上使用了
clang++--std=c++11-Wall-Wextra-o main.cpp
这就是原因!谢谢你们。我第一次真正使用命令行编译。我应该好好读一读。
error: no matching constructor for initialization of
      'std::__1::thread'
    thread t1(task1, "Hello");
#ifndef _LIBCPP_CXX03_LANG
    template <class _Fp, class ..._Args,
              class = typename enable_if
              <
                   !is_same<typename __uncvref<_Fp>::type, thread>::value
              >::type
             >
        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
        explicit thread(_Fp&& __f, _Args&&... __args);
#else  // _LIBCPP_CXX03_LANG
    template <class _Fp>
    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
    explicit thread(_Fp __f);
#endif