为什么新位置依赖于#include
?
听起来很荒谬? 这段代码仅在未注释include:
// #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’
看起来所有候选人都不是新来的。 好像我的placement-new表达式没有被识别一样。 除非我包括那个标题,这是完全荒谬的,因为它是一个语言特性。
编辑:
这对我来说是荒谬的,因为我当然已经阅读了CPPreference.com上的文档(它涵盖了新的位置),其中列出的标题deps是没有的。
为什么新位置依赖于#include
?
它不是。 新位置取决于
。 它依赖于它,因为语言说它依赖于它。 它与运算符new重载有关,该重载由placement new调用,并在
中声明。
恰好为您包含了
。 当然,你不应该依赖它。