c++ - std::atomic 不受 clang 支持?

标签 c++ clang atomic stdatomic

我正在尝试将 std::atomic 与 clang 一起使用。但是,每当我尝试包含头文件原子(#include <atomic>)时,我都会收到消息“找不到原子”。请注意,我包括 - std=c++11 -stdlib=libc++编译时。我错过了什么?

我使用的 clang 版本是 3.2。

最佳答案

The version of clang I'm using is 3.2.

Clang 根据 LLVM CXX Status 添加了跨两个不同版本的原子支持.第一个是 Clang 3.1,第二个是 Clang 3.2。

认为您可以使用以下方式检查它:

#if defined(__clang__)
#  if __has_feature(cxx_atomic)
#    define CLANG_CXX11_ATOMICS 1
#  endif
#endif

然后,在您的代码中:

#if CLANG_CXX11_ATOMICS
# include <atomic>
#endif

...

#if defined(CLANG_CXX11_ATOMICS)
# define MEMORY_BARRIER() std::atomic_thread_fence(std::memory_order_acq_rel)
#elif defined(__GNUC__) || defined(__clang__)
# define MEMORY_BARRIER() __asm__ __volatile__ ("" ::: "memory")
...
#endif

我只能说“我认为” 因为cxx_atomic 没有记录在Clang Language Extensions .但是,它出现在 LLVM 站点的搜索中:"cxx_atomic" site:llvm.org .

CFE 用户邮件列表还有一个悬而未决的问题:How to check for std::atomic availability?


Note that I'm including -std=c++11 -stdlib=libc++ while compiling. What am I missing?

为此,您可能会使用其中一种 Clang/LLVM C++ 运行时,它实际上只是 C++03,但伪装成 C++11。这在过去给我带来了很多问题,因为我们支持许多编译器和平台。

Below is a test Jonathan Wakely helped us craft查看它是否真的是一个 C++11 库,或者是 Apple 的假 C++11 库之一:

// Visual Studio began at VS2010, http://msdn.microsoft.com/en-us/library/hh567368%28v=vs.110%29.aspx.
// Intel and C++11 language features, http://software.intel.com/en-us/articles/c0x-features-supported-by-intel-c-compiler
// GCC and C++11 language features, http://gcc.gnu.org/projects/cxx0x.html
// Clang and C++11 language features, http://clang.llvm.org/cxx_status.html
#if (_MSC_VER >= 1600) || (__cplusplus >= 201103L)
# define CXX11_AVAILABLE 1
#endif

// Hack ahead. Apple's standard library does not have C++'s unique_ptr in C++11. We can't
//   test for unique_ptr directly because some of the non-Apple Clangs on OS X fail the same
//   way. However, modern standard libraries have <forward_list>, so we test for it instead.
//   Thanks to Jonathan Wakely for devising the clever test for modern/ancient versions.
// TODO: test under Xcode 3, where g++ is really g++.
#if defined(__APPLE__) && defined(__clang__)
#  if !(defined(__has_include) && __has_include(<forward_list>))
#    undef CXX11_AVAILABLE
#  endif
#endif

关于c++ - std::atomic 不受 clang 支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25270799/

相关文章:

gcc - gcc和clang在编译时给我一个错误

c++ - 无法使用 llvm 和 clang 解析 C++

redis - 根据条件增加或重置

c++ - 使用 SFINAE 检测同类继承

c++ - 无法在 Windows 7 机器上的 Microsoft Visual C++ 2010 中运行 OpenCV

c++ - 无论对象变量类型如何,如何对对象数组进行排序

python - 安装 `dulwich` 给出 `error: command ' clang' failed with exit status 1`

c++ - 将原子变量传递给函数

c++ - OpenMP自动更新数组值

c++ - 为什么 std::istream 不承担其 streambuf 的所有权?