命名空间 'std' 中没有名为 'hardware_constructive_interference_size' 的成员。

4
根据cppreference,要确定std::hardware_constructive_interference_size是否可用,它使用以下示例:
#include <new>

#ifdef __cpp_lib_hardware_interference_size
    using std::hardware_constructive_interference_size;
    using std::hardware_destructive_interference_size;
#else
    // 64 bytes on x86-64 │ L1_CACHE_BYTES │ L1_CACHE_SHIFT │ __cacheline_aligned │ ...
    constexpr std::size_t hardware_constructive_interference_size
        = 2 * sizeof(std::max_align_t);
    constexpr std::size_t hardware_destructive_interference_size
        = 2 * sizeof(std::max_align_t);
#endif

然而,我的系统定义了__cpp_lib_hardware_interference_size但没有符号std::hardware_constructive_interference_size
我该如何处理这种情况?是否有一种方法可以检查符号是否已定义?
  • Apple clang版本12.0.0(clang-1200.0.32.29)
    目标:x86_64-apple-darwin19.6.0
    线程模型:posix
    InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
  • macOS Catalina 10.15.7(MacBook Pro 2019)

CMakeLists.txt

cmake_minimum_required(VERSION 3.19)
project(untitled4)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
if (UNIX AND NOT APPLE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
endif()

add_executable(untitled4 main.cpp)

如果您打印__cpp_lib_hardware_interference_size,它的值是多少?它是否为>= 201703L?在阅读一个旧的LLVM邮件线程时,我注意到他们非常不愿意将其添加到他们的库中,并考虑向标准委员会编写缺陷报告。 - Ted Lyngmo
它打印出了“201703”。这很奇怪...根据https://dev59.com/l1kS5IYBdhLWcg3wj3jP#39887282上的评论,提案的作者在苹果Clang团队工作。 - 김선달
1
好的,提案必须来自于某个地方 :-) 据我所知,gcc也没有实现这个功能。但是我很惊讶这个特性测试宏“说谎了”。我的clang++-12(在Linux上)没有定义那个宏。 - Ted Lyngmo
1个回答

2

我该如何处理这种情况?

您可以使用预定义的宏检测损坏的语言实现,并对其进行异常处理。

可以通过尝试编译和运行一个小程序来进行检测:

#include <new>

int main() {
#ifdef __cpp_lib_hardware_interference_size
    // return 0 if the interference_sizes are defined
    return !(std::hardware_constructive_interference_size &&
             std::hardware_destructive_interference_size);
#else
    return 1; // no interference_sizes
#endif
}

clang++ -std=c++17 -o hwisize hwisize.cpp 2>/dev/null && ./hwisize
has_hw_interference_sizes=$?
  • 如果编译失败,has_hw_interference_sizes 将会是 1
  • 如果编译成功,但未定义 __cpp_lib_hardware_interference_sizehas_hw_interference_sizes 将会是 1
  • 如果编译成功,并且已经定义了 __cpp_lib_hardware_interference_sizehas_hw_interference_sizes 将会是 0

(反向的 bool 逻辑是常见 shell 定义 true(0)和 false(非 0)的方式)

只需将其插入到您的构建系统中即可。


如何使用预定义的宏来检测破损的语言实现? - 김선달
@김선달,我在答案中添加了一种方法。希望eerorika对此没有意见。 - Ted Lyngmo
@TedLyngmo 谢谢!很遗憾,语言本身无法处理这个问题。 - 김선달
@김선달 特性测试宏应该足够,但如果实现中存在缺陷,则除了实际测试之外,很少有其他方法可以解决。 - Ted Lyngmo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接