我试图编译一个C++代码,使用过对齐的变量.如果我try 编译以下代码(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都被赋予编译器),那么一切都会顺利运行.然而,在使用英特尔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版文件:

在本版本的编译器中,所有必要的内容包括:

#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的文件中缺失.

Linux相关问答推荐

如何查明第三方是否杀死或 destruct 了 C++ 中的程序

从父目录中删除目录而不删除父目录 (Linux)

Powershell Core,MXLinuxv21,运行 linux /usr/bin/x 命令导致对象命令运行时找不到文件

如何使用 ftrace 过滤查看函数上方的调用堆栈?

用于替换 struct 文档中文本的 Bash 脚本

命令应在终端关闭后继续运行

Linux 如何将主机文件作为用户的输入并将其调用到脚本中

Bash - 如何根据 names.txt 重命名目录中的文件

Linux合并文件

Linux 进程在后台 - 在作业(job)中 Stopped停止?

在不调试的情况下查找哪个汇编指令导致了非法指令错误

有没有办法在整个项目代码中的某个日期之后找出更改的文件?

从Linux中的行尾删除空格

列出当前目录和所有子目录中特定大小的文件

命令列出除 . (点)和..(点点)

Supervisord - 将进程标准输出重定向到控制台

如何在字符串中查找子字符串(或如何 grep 变量)?

ngrok 如何在防火墙后工作?

如何在 IE 中使用 Linux 进行测试

如何将所有文件从一个目录移动(和覆盖)到另一个目录?