Skip to content

Commit

Permalink
Allow const qualified FunctionRef instances. This allows the signatur…
Browse files Browse the repository at this point in the history
…e to be compatible with AnyInvokable for const uses.

PiperOrigin-RevId: 565682320
Change-Id: I924dadf110481e572bdb8af0111fa62d6f553d90
  • Loading branch information
Abseil Team authored and copybara-github committed Sep 15, 2023
1 parent 9a592ab commit e68f141
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions absl/functional/function_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions absl/functional/function_ref_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e68f141

Please sign in to comment.