相关疑难解决方法(0)

自 C++20 以来,是否允许对分配的存储进行指针运算?

在 C++20 标准中,说数组类型是隐式生命周期类型

这是否意味着可以隐式创建非隐式生命周期类型的数组?这种数组的隐式创建不会导致数组元素的创建?

考虑这个案例:

//implicit creation of an array of std::string 
//but not the std::string elements:
void * ptr = operator new(sizeof (std::string) * 10);
//use launder to get a "pointer to object" (which object?)
std::string * sptr = std::launder(static_cast<std::string*>(ptr));
//pointer arithmetic on not created array elements well defined?
new (sptr+1) std::string("second element");
Run Code Online (Sandbox Code Playgroud)

从 C++20 开始,这段代码不再是 UB 了吗?


也许这种方式更好?

//implicit creation of an array of std::string 
//but not the std::string elements:
void * ptr = operator new(sizeof …
Run Code Online (Sandbox Code Playgroud)

c++ arrays lifetime dynamic-arrays c++20

13
推荐指数
1
解决办法
339
查看次数

标签 统计

arrays ×1

c++ ×1

c++20 ×1

dynamic-arrays ×1

lifetime ×1