Mar*_* Ba 17 c++ strict-aliasing placement-new primitive-types language-lawyer
似乎有一些协议,char由于C++别名规则,你不能毫无疑问地将一个int(一个int*)指向一个数组.
从另一个问题 - 基于通用char []的存储和避免严格别名相关的UB - 它似乎允许(重新)通过新的放置使用存储.
alignas(int) char buf[sizeof(int)];
void f() {
// turn the memory into an int: (??) from the POV of the abstract machine!
::new (buf) int; // is this strictly required? (aside: it's obviously a no-op)
// access storage:
*((int*)buf) = 42; // for this discussion, just assume the cast itself yields the correct pointer value
}
Run Code Online (Sandbox Code Playgroud)
那么,上面是合法的C++ 并且是实际需要的新版本才能使其合法化吗?
| 归档时间: |
|
| 查看次数: |
870 次 |
| 最近记录: |