8.7.1 C++ 实现

01-C++ 实现 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/unsafe-deep-dive/pinning/self-referential-buffer/cpp.html

8.7.1 C++ 实现

#include <cstddef>
#include <cstring>

class SelfReferentialBuffer {
    std::byte data[1024];
    std::byte* cursor = data;
    
public:
    SelfReferentialBuffer(SelfReferentialBuffer&& other) 
        : cursor{data + (other.cursor - other.data)}
    {
        std::memcpy(data, other.data, 1024);
    }
};

可在 Compiler Explorer 上查看

SelfReferentialBuffer 包含两个成员:data 是一 KB 内存,cursor 是指向 data 的指针。

其移动构造函数确保 cursor 更新到新的内存地址。

这种类型在 Rust 中不易表达。

最后修改 August 11, 2026: 更新 (70a5af133)