- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 cppreference 并在 std::aligned_storage 的示例中有一个 vector/数组类的例子:
template<class T, std::size_t N>
class static_vector
{
// properly aligned uninitialized storage for N T's
typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];
// IF you want to see possible implementation of aligned storage see link.
// It's very simple and small, it's just a buffer
std::size_t m_size = 0;
public:
// Create an object in aligned storage
template<typename ...Args> void emplace_back(Args&&... args)
{
if( m_size >= N ) // possible error handling
throw std::bad_alloc{};
// construct value in memory of aligned storage
// using inplace operator new
new(&data[m_size]) T(std::forward<Args>(args)...);
++m_size;
}
// Access an object in aligned storage
const T& operator[](std::size_t pos) const
{
// note: needs std::launder as of C++17
// WHY
return *reinterpret_cast<const T*>(&data[pos]);
}
// Delete objects from aligned storage
~static_vector()
{
for(std::size_t pos = 0; pos < m_size; ++pos) {
// note: needs std::launder as of C++17
// WHY?
reinterpret_cast<T*>(&data[pos])->~T();
}
}
};
本质上,每个元素所在的每个存储区/区域/内存地址都是一个字符缓冲区。在每个存在的元素位置,在 T 类型的缓冲区中完成一个新的放置。因此,当返回该缓冲区的位置时,可以将 char 缓冲区强制转换为 T*,因为在那里构造了一个 T。为什么从 C++17 开始需要 std::launder?我已经问了很多关于这个的问题,似乎同意:
alignas(alignof(float)) char buffer [sizeof(float)];
new (buffer) float;// Construct a float at memory, float's lifetime begins
float* pToFloat = buffer; // This is not a strict aliasing violation
*pToFloat = 7; // Float is live here
这显然不是严格的别名违规,完全没问题。那么为什么这里需要 std::launder 呢?
最佳答案
// note: needs std::launder as of C++17
return *reinterpret_cast<const T*>(&data[pos]);
Why is std::launder required from C++17 onwards?
reinterpret_cast
取消引用指针的条件是最小的。这不是其中的一个。您正在将指向一种类型的指针转换为指向完全不相关类型的指针,并通读它。见
here .
std::launder
允许您派生有效的指针。存在的全部理由就是给你这个。
T
的指针。就在眼前,之前
std::launder
你不能从缓冲区制作一个。
I have asked a bunch of questions about this and it seems agreed that... This is not a strict aliasing violation apparently, it's completely fine. So therefore why is std::launder required here?
关于c++ - 为什么这里需要 std::launder ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68869404/
我正在阅读 cppreference 并在 std::aligned_storage 的示例中有一个 vector/数组类的例子: template class static_vector {
我正在为 Game Boy Advance 使用 C++ 做一个有点不平凡的项目,而且,作为一个完全没有内存管理的有限平台,我试图避免调用 malloc和动态分配。为此,我实现了相当多的“就地多态容器
我想确认我是否正确使用 std::launder(...)确保我正确理解它的用法。 我正在创建一个 Result在基于 Rust 实现的 C++ 中。 template class ResultSt
考虑这段代码: void f(char * ptr) { auto int_ptr = reinterpret_cast(ptr); // (&i)); } void example_2()
考虑以下固定大小 vector 的简化且不完整的实现: template class Vec { T *start, *end; public: T& operator[](ssize_t i
P0137引入了函数模板 std::launder 并在有关 union 、生命周期和指针的部分中对标准进行了许多更改。 本文要解决的问题是什么?我必须注意的语言变化是什么?我们在洗钱做什么? 最佳答
std::launder 函数要求通过结果可访问的每个字节都可通过参数访问。 “可达”定义如下: A byte of storage is reachable through a pointer va
这是我不明白的示例部分: struct Y { int z; }; int main() { alignas(Y) std::byte s[sizeof(Y)]; Y *q =
当前标准草案(大概是 C++17)在 [basic.compound/4] 中说: [ Note: An array object and its first element are not poin
这个问题跟在这个 one 之后 让我们考虑这个示例代码: struct sso { union{ struct { char* ptr; char size_r
当前的标准草案(大概是 C++17)在 [basic.compound/4] 中说: [ Note: An array object and its first element are not poi
在 const_cast 之后修改一个 const 构造的对象是 UB(我相信是由于不断传播)。即使与 std::launder 结合使用,它仍然是 UB 吗? (哪个 AFAIK 阻止了一些优化,例
[1] 有没有添加p0593r6的情况转化为 C++20(第 6.7.2.11 节对象模型 [intro.object])制作 std::launder没有必要,在 C++17 中需要相同的用例 st
我想知道为什么 std::launder 是一个 constexpr 函数。有没有可以在编译时使用的用例? 最佳答案 因为绝对没有理由不这样做。它实际上只是对编译器具有一些特殊附加含义的恒等函数。它不
我必须使用不同版本的 clang 编译相同的代码。由于代码包含一些 c++17 功能,并非每个版本的 clang 都支持这些功能,因此我想在编译时检查它们是否受支持。据我所知,clang 的 feat
它类似于 std::optional,但不存储额外的 bool。用户必须确保只有在初始化后才能访问。 template union FakeOptional { //Could be a normal
对于 feature request的 a project of mine ,我想/有人建议我使用 std::launder 在 vector 中移动元素,其中元素只能移动可构造(未定义移动赋值运算符
考虑这个 union : union A{ int a; struct{ int b; } c; }; c和 a不是 layout-compatibles类型,因此无法读取
此问题是对 Is adding to a "char *" pointer UB, when it doesn't actually point to a char array? 的后续问题。 在 C
这个问题在这里已经有了答案: Does this really break strict-aliasing rules? (3 个答案) 关闭 5 年前。 以下例子来自std::aligned_st
我是一名优秀的程序员,十分优秀!