考虑下面的代码:的sizeof的std :: aligned_storage和std :: aligned_union
#include <iostream>
#include <type_traits>
int main() {
std::aligned_storage<sizeof(double), alignof(double)> storage;
std::aligned_union<sizeof(double), double> union_storage;
std::cout << sizeof(storage) << '\n';
std::cout << sizeof(union_storage) << '\n';
std::cout << sizeof(double) << '\n';
}
我希望sizeof(storage)
和sizeof(union_storage)
为大于或等于sizeof(double)
,因为他们必须要能够保持double
。然而,I get the output
1
1
8
铛-3.8和gcc-5.3都产生此输出。
为什么sizeof
返回不正确的尺寸?
如果我使用placement new将double
放入storage
或union_storage
那会是未定义的行为吗?
或'aligned_storage_t'等在C++中14。 –