首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译过对齐动态分配变量时的icpc错误

编译过对齐动态分配变量时的icpc错误
EN

Stack Overflow用户
提问于 2022-03-30 08:29:50
回答 1查看 88关注 0票数 3

我试图在C++中编译一个代码,它使用过对齐变量.如果我试图编译以下代码( MWE)

代码语言:javascript
复制
#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编译时,我得到了以下错误

代码语言:javascript
复制
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)

我不明白这个错误会是什么。我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-30 08:55:17

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

在编译器的这个版本中,为了获得对齐数据的正确的动态分配,

所必需的所有都包括一个新的标头

#include <aligned_new>

在包含此标头之后,任何对齐类型的新表达式将自动分配该类型对齐的内存。

链接: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的文档中缺少本节。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71673959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档