Skip to content

fix: bad_expected_access::what() returns the error message - #1269

Merged
facontidavide merged 3 commits into
BehaviorTree:masterfrom
azerbenali:fix-expected-what
Nov 7, 2026
Merged

facontidavide merged 3 commits into
BehaviorTree:masterfrom
azerbenali:fix-expected-what

Conversation

@azerbenali

Copy link
Copy Markdown
Contributor

Fixes #1263

what() now returns the error when it converts to std::string, otherwise the old message. Kept C++11-compatible (tag dispatch instead of if constexpr) since the header targets C++11, and the message is in a shared_ptr so copying the exception doesn't allocate.

Note: with C++23 nonstd::expected maps to std::expected, which this doesn't change.

azerbenali and others added 3 commits October 28, 2026 00:33
…ree#1263)

what() returned a fixed "bad_expected_access", so the actual error of a
failed Expected<T> was lost. When the error type converts to std::string,
the message is now returned instead. It is stored in a shared_ptr so that
copying the exception does not allocate.
…nd copy

Return m_error.c_str() through an overload on std::string, with a generic
fallback for other error types. No extra member, so the class layout is
unchanged, and copying the exception no longer copies the message twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@facontidavide

Copy link
Copy Markdown
Collaborator

Thanks @azerbenali, this is a real usability win: getInput<T>(...).value() failing with a bare bad_expected_access is a common complaint.

I pushed one commit (dcea354) on top of a merge of master that simplifies it. m_error already holds the string, so what() can return m_error.c_str() through an overload on std::string, with a template fallback for other error types. The shared_ptr member isn't needed: copying the exception copies m_error anyway. Without the extra member the layout of this public template class stays unchanged. The // BT.CPP patch marker flags the edit to the vendored header for whoever re-vendors it. Your tests pass unchanged (598/598 locally). I'll squash-merge once CI is green.

@azerbenali

Copy link
Copy Markdown
Contributor Author

Thanks Davide! Appreciate the improvements and your help!

@facontidavide
facontidavide merged commit 64cac39 into BehaviorTree:master Nov 7, 2026
17 checks passed
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.

bad_expected_access::what() returns a hardcoded string instead of the actual error message

2 participants