Skip to content

Make smart pointer operator bool() explicit - #11

Merged
zhuuuoyue merged 2 commits into
mainfrom
copilot/fix-smart-pointer-explicit-bool
Apr 8, 2026
Merged

Make smart pointer operator bool() explicit#11
zhuuuoyue merged 2 commits into
mainfrom
copilot/fix-smart-pointer-explicit-bool

Conversation

Copilot AI commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Non-explicit operator bool() on smart pointers allows silent implicit conversions to integral types, enabling subtle bugs (e.g., pointer arithmetic, unintended overload resolution, comparisons that compile but don't do what's intended).

Changes

  • ShadowPtr and OwnerPtr (include/PrettyMemory.h): mark both operator bool() overloads explicit
// Before
operator bool() const { return nullptr != Get(); }

// After
explicit operator bool() const { return nullptr != Get(); }

Contextual conversions (if, while, !, &&, ||, GoogleTest EXPECT_TRUE/EXPECT_FALSE) continue to work — implicit conversion in non-boolean contexts is now rejected by the compiler.

Copilot AI changed the title [WIP] Fix bool operator in smart pointer to be explicit Make smart pointer operator bool() explicit Apr 8, 2026
Copilot AI requested a review from zhuuuoyue April 8, 2026 06:46
@zhuuuoyue
zhuuuoyue marked this pull request as ready for review April 8, 2026 06:53
@zhuuuoyue
zhuuuoyue merged commit 61893d8 into main Apr 8, 2026
1 check passed
@zhuuuoyue
zhuuuoyue deleted the copilot/fix-smart-pointer-explicit-bool branch April 8, 2026 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

智能指针的 bool 运算符应该声明为 explicit 的,避免隐式转换引入错误

2 participants