Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/include/llvm/ADT/STLForwardCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <optional>
#include <type_traits>
#include <utility>

namespace llvm {

Expand Down Expand Up @@ -117,6 +118,15 @@ struct detector<std::void_t<Op<Args...>>, Op, Args...> {
template <template <class...> class Op, class... Args>
using is_detected = typename detail::detector<void, Op, Args...>::value_t;

struct identity_cxx20 // NOLINT(readability-identifier-naming)
{
using is_transparent = void;

template <typename T> constexpr T &&operator()(T &&self) const noexcept {
return std::forward<T>(self);
}
};

//===----------------------------------------------------------------------===//
// Features from C++23
//===----------------------------------------------------------------------===//
Expand Down
22 changes: 22 additions & 0 deletions llvm/unittests/ADT/STLForwardCompatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,26 @@ TEST(TransformTest, ToUnderlying) {
static_assert(llvm::to_underlying(E3::B3) == 0);
}

TEST(STLForwardCompatTest, IdentityCxx20) {
llvm::identity_cxx20 identity;

// Test with an lvalue.
int X = 42;
int &Y = identity(X);
EXPECT_EQ(&X, &Y);

// Test with a const lvalue.
const int CX = 10;
const int &CY = identity(CX);
EXPECT_EQ(&CX, &CY);

// Test with an rvalue.
EXPECT_EQ(identity(123), 123);

// Test perfect forwarding.
static_assert(std::is_same_v<int &, decltype(identity(X))>);
static_assert(std::is_same_v<const int &, decltype(identity(CX))>);
static_assert(std::is_same_v<int &&, decltype(identity(int(5)))>);
}

} // namespace