新的放置取决于 iostream
Placement new depends on iostream
为什么放置新取决于#include <iostream>
?
听起来很荒谬?好吧,此代码仅在注释包含时编译:
// #include <iostream>
struct Alignas { void* ptr; };
alignas(Alignas) static char storage[sizeof(Alignas)];
int main() { new(storage) Alignas; }
Gcc 错误(与 Clang 相同(:
alignas.cpp:7:27: error: no matching function for call to ‘operator new(sizetype, char [8])’
7 | int main() { new(storage) Alignas; }
| ^~~~~~~
<built-in>: note: candidate: ‘void* operator new(long unsigned int)’
<built-in>: note: candidate expects 1 argument, 2 provided
<built-in>: note: candidate: ‘void* operator new(long unsigned int, std::align_val_t)’
<built-in>: note: no known conversion for argument 2 from ‘char [8]’ to ‘std::align_val_t’
看起来没有一个候选人是新安置的。好像我的放置新表达式无法识别。除非我包含该标题,这是完全荒谬的,因为它是一种语言功能。
编辑:
这对我来说很荒谬,因为我当然已经阅读了有关 cppreference.com 的文档(其中涵盖了放置新内容(,并且列出的标题部门没有。
为什么放置新取决于
#include <iostream>
?
其实不然。新放置取决于<new>
。它取决于它,因为语言说它依赖于它。它与运算符新重载有关,这些重载由放置 new 调用并在<new>
中声明。
<iostream>
恰好为你提供了<new>
。当然,你不应该依赖它。
相关文章:
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话
- 需要从 istream 和 ostream 派生 iostream
- 重载运算符new[]的行为取决于析构函数
- std::带有自定义缓冲区的 iostream 不允许我写入
- 新的放置取决于 iostream
- 包含在 <initializer_list> <iostream>?
- Writefile() 无法写入数据,具体取决于数据的长度
- 为什么在包含iostream时可以使用printf()?
- ASIO signal_set多个 IO 线程不可靠,具体取决于代码顺序?
- SFINAE是否取决于类型推断?
- 将强制转换简化为取决于参数的类型
- 修复"-Wunused-parameter"取决于预处理器条件的警告
- 内存中类位置的成员是否取决于类成员在类定义中的位置?
- 如果 iostream 对象不可复制,为什么以下代码是合法的?
- 为什么我必须在包含后写 std::cin <iostream>?
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- 磁力计代码:C++ vs C - iosstream,矢量 - 错误iostream:没有这样的文件或目录
- 奇怪的Arduino C++编译错误取决于文件位置
- 通过boost asio iostream下载大文件的最快方法是什么?
- CUDA 的性能取决于声明变量