stl: functional.hpp for identity

This commit is contained in:
skypjack
2025-12-22 10:22:46 +01:00
parent b469b2b353
commit 09b9099b1c
5 changed files with 51 additions and 0 deletions

View File

@@ -194,6 +194,7 @@ if(ENTT_INCLUDE_HEADERS)
signal/emitter.hpp
signal/fwd.hpp
signal/sigh.hpp
stl/functional.hpp
stl/memory.hpp
tools/davey.hpp
entt.hpp

View File

@@ -65,5 +65,6 @@ namespace entt {}
#include "signal/dispatcher.hpp"
#include "signal/emitter.hpp"
#include "signal/sigh.hpp"
#include "stl/functional.hpp"
#include "stl/memory.hpp"
// IWYU pragma: end_exports

View File

@@ -0,0 +1,35 @@
#ifndef ENTT_STL_FUNCTIONAL_HPP
#define ENTT_STL_FUNCTIONAL_HPP
#include "../config/config.h"
namespace entt::stl {
#ifndef ENTT_FORCE_STL
# if __has_include(<version>)
# include <version>
#
# if defined(__cpp_lib_ranges)
# define ENTT_HAS_IDENTITY
# include <functional>
using std::identity;
# endif
# endif
#endif
#ifndef ENTT_HAS_IDENTITY
# include <utility>
struct identity {
using is_transparent = void;
template<typename Type>
[[nodiscard]] constexpr Type &&operator()(Type &&value) const noexcept {
return std::forward<Type>(value);
}
};
#endif
} // namespace entt::stl
#endif

View File

@@ -304,4 +304,5 @@ SETUP_BASIC_TEST(sigh entt/signal/sigh.cpp)
# Test stl
SETUP_BASIC_TEST(stl_functional entt/stl/functional.cpp ENTT_USE_STL)
SETUP_BASIC_TEST(stl_memory entt/stl/memory.cpp ENTT_USE_STL)

View File

@@ -0,0 +1,13 @@
#include <memory>
#include <gtest/gtest.h>
#include <entt/core/type_traits.hpp>
#include <entt/stl/functional.hpp>
TEST(Identity, Functionalities) {
const entt::stl::identity identity;
int value = 2;
ASSERT_TRUE(entt::is_transparent_v<entt::stl::identity>);
ASSERT_EQ(identity(value), value);
ASSERT_EQ(&identity(value), &value);
}