c – 放置新的虚假警告?
发布时间:2020-12-16 03:33:21 所属栏目:百科 来源:网络整理
导读:我有以下代码: #include "type_traits"#include newvoid foo(){ std::aligned_storage10,alignof(long) storage; new (storage) int(12);} 我定义了一些存储(长度为10个字节),并且我将int添加到该位置 gcc 7.3给出以下警告: source:10:10: warning: placem
我有以下代码:
#include "type_traits" #include <new> void foo() { std::aligned_storage<10,alignof(long)> storage; new (&storage) int(12); } 我定义了一些存储(长度为10个字节),并且我将int添加到该位置 gcc 7.3给出以下警告: <source>:10:10: warning: placement new constructing an object of type 'int' and size '4' in a region of type 'std::aligned_storage<10,8>' and size '1' [-Wplacement-new=] new (&storage) int(12); 如果我不正确,则此警告不正确.我在这里遗漏了什么或这个警告是虚假的吗? 解决方法
std :: aligned_storage是具有嵌套类型成员的特征.该类型具有正确的大小和对齐方式.特征本身可能没有数据成员,因此将获得默认对象大小,即1.
所以修复很简单: std::aligned_storage<10,alignof(long)>::type storage; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |