aboutsummaryrefslogtreecommitdiff
path: root/absl/functional/any_invocable.h
diff options
context:
space:
mode:
authorDino Radakovic <dinor@google.com>2024-05-07 19:09:38 -0700
committerCopybara-Service <copybara-worker@google.com>2024-05-07 19:10:24 -0700
commite444af7ccc82549c97ebbc17e8af435ccacdc4e2 (patch)
treea5b5e517d8684af447ee950afeac42dd70e1df57 /absl/functional/any_invocable.h
parent289d8626b6ff1e2a7bd582b43c918345108f5fda (diff)
downloadabseil-e444af7ccc82549c97ebbc17e8af435ccacdc4e2.tar.gz
abseil-e444af7ccc82549c97ebbc17e8af435ccacdc4e2.tar.bz2
abseil-e444af7ccc82549c97ebbc17e8af435ccacdc4e2.zip
`any_invocable`: Add public documentation for undefined behavior when invoking an empty AnyInvocable
This is currently documented in `internal/any_invocable.h`, but not in the public API. For example, `std::function` [documents](https://en.cppreference.com/w/cpp/utility/functional/function) the fact that invoking an empty instance throws `std::bad_function_call`. PiperOrigin-RevId: 631621604 Change-Id: I6b886a805ffa0e7aaef5f6971e1eafd14f94050c
Diffstat (limited to 'absl/functional/any_invocable.h')
-rw-r--r--absl/functional/any_invocable.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/absl/functional/any_invocable.h b/absl/functional/any_invocable.h
index 176077ae..bc980731 100644
--- a/absl/functional/any_invocable.h
+++ b/absl/functional/any_invocable.h
@@ -151,6 +151,12 @@ ABSL_NAMESPACE_BEGIN
//
// Attempting to call `absl::AnyInvocable` multiple times in such a case
// results in undefined behavior.
+//
+// Invoking an empty `absl::AnyInvocable` results in undefined behavior:
+//
+// // Create an empty instance using the default constructor.
+// AnyInvocable<void()> empty;
+// empty(); // WARNING: Undefined behavior!
template <class Sig>
class AnyInvocable : private internal_any_invocable::Impl<Sig> {
private:
@@ -167,6 +173,7 @@ class AnyInvocable : private internal_any_invocable::Impl<Sig> {
// Constructors
// Constructs the `AnyInvocable` in an empty state.
+ // Invoking it results in undefined behavior.
AnyInvocable() noexcept = default;
AnyInvocable(std::nullptr_t) noexcept {} // NOLINT
@@ -277,6 +284,8 @@ class AnyInvocable : private internal_any_invocable::Impl<Sig> {
// In other words:
// std::function<void()> f; // empty
// absl::AnyInvocable<void()> a = std::move(f); // not empty
+ //
+ // Invoking an empty `AnyInvocable` results in undefined behavior.
explicit operator bool() const noexcept { return this->HasValue(); }
// Invokes the target object of `*this`. `*this` must not be empty.