Skip to content

Commit 61c3889

Browse files
committed
Fix missing forwarding reference
1 parent 5023264 commit 61c3889

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

libc/src/__support/CPP/type_traits/invoke.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@
1010
#define LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_INVOKE_H
1111

1212
#include "src/__support/CPP/type_traits/decay.h"
13+
#include "src/__support/CPP/type_traits/enable_if.h"
1314
#include "src/__support/CPP/type_traits/is_base_of.h"
15+
#include "src/__support/CPP/type_traits/is_same.h"
1416
#include "src/__support/CPP/utility/forward.h"
1517

16-
// BEWARE : this implementation is not fully conformant as it doesn't take
17-
// `cpp::reference_wrapper` into account.
18-
1918
namespace __llvm_libc::cpp {
2019

2120
namespace detail {
2221

23-
// Catch all function types.
22+
// Catch all function and functor types.
2423
template <class FunctionPtrType> struct invoke_dispatcher {
25-
template <class... Args>
26-
static auto call(FunctionPtrType &&fun, Args &&...args) {
27-
return cpp::forward<FunctionPtrType>(fun)(cpp::forward<Args>(args)...);
24+
template <class T, class... Args,
25+
typename = cpp::enable_if_t<
26+
cpp::is_same_v<cpp::decay_t<T>, FunctionPtrType>>>
27+
static auto call(T &&fun, Args &&...args) {
28+
return cpp::forward<T>(fun)(cpp::forward<Args>(args)...);
2829
}
2930
};
3031

libc/test/src/__support/CPP/type_traits_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ TEST(LlvmLibcTypeTraitsTest, invoke) {
196196
{ // lambda
197197
EXPECT_EQ(cpp::invoke([]() -> int { return 2; }), 2);
198198
EXPECT_EQ(cpp::invoke([](int value) -> int { return value; }, 1), 1);
199+
200+
const auto lambda = [](int) { return 0; };
201+
EXPECT_EQ(cpp::invoke(lambda, 1), 0);
199202
}
200203
}
201204

0 commit comments

Comments
 (0)