gpt4 book ai didi

c++ - 编译过度对齐的动态分配变量时出现 icpc 错误

转载 作者:行者123 更新时间:2023-12-05 05:44:01 26 4
gpt4 key购买 nike

我正在尝试用 C++ 编译一段代码,它使用了过度对齐的变量。如果我尝试编译以下代码(MWE)

#include <new>
#include <iostream>

int main()
{
alignas(32) double *r = new (std::align_val_t{32}) double[3];
std::cout << "alignof(r) is " << alignof(r) << '\n';
return 0;
}

如果我使用 icpxg++ 一切都会顺利运行(在所有情况下,都会给出标志 -std=c++17给编译器)。但是,在使用Intel icpc编译时,出现如下错误

mwe.cpp(6): error: no instance of overloaded "operator new[]" matches the argument list
argument types are: (unsigned long, std::align_val_t)
alignas(32) double *r = new (std::align_val_t{32}) double[3];
^
/usr/include/c++/9/new(175): note: this candidate was rejected because arguments do not match
_GLIBCXX_NODISCARD inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
^
/usr/include/c++/9/new(141): note: this candidate was rejected because arguments do not match
_GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
^
/usr/include/c++/9/new(127): note: this candidate was rejected because mismatch in count of arguments
_GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
^

mwe.cpp(7): warning #3464: the standard "alignof" operator does not accept an expression argument (use a type instead)
std::cout << "alignof(r) is " << alignof(r) << '\n';
^

compilation aborted for mwe.cpp (code 2)

而且我不明白错误可能是什么。我该如何解决这个问题?

最佳答案

看来 icpc 不符合对齐分配的标准。引用 2021.5 版本的文档:

In this release of the compiler, all that is necessary in order to get correct dynamic allocation for aligned data is to include a new header:

#include <aligned_new>

After this header is included, a new-expression for any aligned type will automatically allocate memory with the alignment of that type.

链接:https://www.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/optimization-and-programming-guide/automatically-aligned-dynamic-allocation/automatically-aligned-dynamic-allocation-1.html

现场演示:https://godbolt.org/z/5xMqKGrTG

icpx 文档中缺少此部分:https://www.intel.com/content/www/us/en/develop/documentation/oneapi-dpcpp-cpp-compiler-dev-guide-and-reference/top.html .

关于c++ - 编译过度对齐的动态分配变量时出现 icpc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71673959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com