Is there any interest for `std::unique_ptr` analogue with weak-semantic?
まだ誰も着手していません。
評価
調査の方向性
リンクされた weakable_unique_ptr.hpp のプロトタイプと、Boost.SmartPtr ライブラリの規約を確認することから始めます。weakable_unique_ptr と unique_weak_ptr が Boost に適合するか、それらをどこに配置すべきか、列挙された制限のうちどれに対処する必要があるかを判断してください。完了とみなすには、合意された設計、Boost スタイルの実装、網羅的なテスト、ドキュメントが必要です。
索引モデルが issue の本文から書いたものです。
説明
Hi everyone,
I’ve implemented a weak-reference-enabled version of std::unique_ptr, called weakable_unique_ptr. The core idea is to combine the strict ownership semantics of std::unique_ptr with the ability to create non-owning observers (called unique_weak_ptr), without introducing shared ownership or thread-safety overhead.
Key Properties
- Exclusive Ownership: The owned object is destroyed when the
weakable_unique_ptris destroyed (likestd::unique_ptr); - Observation:
unique_weak_ptrobservers automatically expire when the owned object is destroyed (likestd::weak_ptr); - Less cost: no copy operation for
weakable_unique_ptrat all,unique_weak_ptr's reference counter without atomic operations.
Implementation Status
Attached is a working prototype of weakable_unique_ptr and unique_weak_ptr, tested on Godbolt's GCC 15.1.
The current implementation is standalone (no modifications to Boost.SmartPtr library required) and supports:
- Move-only semantics (like
std::unique_ptr); - Safe validity checks via
try_get()orexpired().
The current implementation doesn't support yet:
- Handling array types;
- Handling deleter as reference;
- EBO for deleter;
- Lazy initalization for control block(BTW do we really need to support it?);
- Type erasing for control block(in order not to pollute binary);
- Construction and assignment from
std::unique_ptr; operator[]for array types;- implicit conversion to
std::shared_ptr.
Use Case Example
struct Node {
boost::weakable_unique_ptr<Node> child;
boost::unique_weak_ptr<Node> parent; // Safe back-reference
};
void demo() {
auto root = boost::make_weakable_unique<Node>();
auto child = boost::make_weakable_unique<Node>();
child->parent = root; // Safe link
root->child = std::move(child);
} // All auto-destructs, no leaks
This is useful for:
- Trees/ASTs with parent links.
- GUI hierarchies (e.g., widgets owning children but needing parent access).
- Any graph structure where ownership is linear but observation is needed.
Next Steps
If there’s interest, I’d love to:
- Polish the code to Boost standards.
- Add exhaustive tests/docs.
- Integrate with Boost.SmartPtr library.
Questions for the community:
- Does this align with Boost’s goals?
- Should it live under Boost.SmartPtr library or as a standalone utility?
- Any design concerns?
weakable_unique_ptr
The full snippet to play: https://godbolt.org/z/41535cTvo
- 主要言語
- C++
- スター
- 111
- フォーク
- 116
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
boostorg/smart_ptr のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
boostorg/smart_ptr の issue をすべて見る
似ている issue
-
Remove source files from packaging for deb, zip, etc対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
OpenSpace/OpenSpace#4353 · コメント 1 件 ·
メンテナーはふだん 5 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
justcallmekoko/ESP32Marauder#1656 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
macOS: a double-click on a notarized altairsim is refused, and the docs give only the offline case対応中かも @anhaoXie が今日担当しました。 オープンdocumentation
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
Decompiler: convertCharRef() in xml.y overflows its int4 accumulator on long character references対応中かも @kaayzouee が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
NationalSecurityAgency/ghidra#10009 ·
メンテナーはふだん 2 日以内に返信