summaryrefslogtreecommitdiff
path: root/absl/functional
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-09-15 08:14:33 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-09-15 08:15:17 -0700
commite68f1412e2cebdd5af106721007335ca45d51f83 (patch)
treead5dc9666ec23e9ef0e68f21817829c2b160b0e2 /absl/functional
parent9a592abd7c7508386666bdbcd00d8bc030bc966e (diff)
Allow const qualified FunctionRef instances. This allows the signature to be compatible with AnyInvokable for const uses.
PiperOrigin-RevId: 565682320 Change-Id: I924dadf110481e572bdb8af0111fa62d6f553d90
Diffstat (limited to 'absl/functional')
-rw-r--r--absl/functional/function_ref.h8
-rw-r--r--absl/functional/function_ref_test.cc5
2 files changed, 13 insertions, 0 deletions
diff --git a/absl/functional/function_ref.h b/absl/functional/function_ref.h
index 2b9139d3..96cece55 100644
--- a/absl/functional/function_ref.h
+++ b/absl/functional/function_ref.h
@@ -137,6 +137,14 @@ class FunctionRef<R(Args...)> {
absl::functional_internal::Invoker<R, Args...> invoker_;
};
+// Allow const qualified function signatures. Since FunctionRef requires
+// constness anyway we can just make this a no-op.
+template <typename R, typename... Args>
+class FunctionRef<R(Args...) const> : public FunctionRef<R(Args...)> {
+ public:
+ using FunctionRef<R(Args...)>::FunctionRef;
+};
+
ABSL_NAMESPACE_END
} // namespace absl
diff --git a/absl/functional/function_ref_test.cc b/absl/functional/function_ref_test.cc
index c61117eb..c0211135 100644
--- a/absl/functional/function_ref_test.cc
+++ b/absl/functional/function_ref_test.cc
@@ -47,6 +47,11 @@ TEST(FunctionRefTest, Function2) {
EXPECT_EQ(1337, ref());
}
+TEST(FunctionRefTest, ConstFunction) {
+ FunctionRef<int() const> ref(Function);
+ EXPECT_EQ(1337, ref());
+}
+
int NoExceptFunction() noexcept { return 1337; }
// TODO(jdennett): Add a test for noexcept member functions.