Skip to content

Commit

Permalink
Adding remove_cvref and type_identity
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Feb 14, 2024
1 parent 2712ff2 commit e5ee470
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmake/unit_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ endfunction()
#######################################################################

add_subdirectory( src/tools/Log/test )

add_subdirectory( src/tools/std20/type_traits/test )
3 changes: 2 additions & 1 deletion src/tools.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tools/overload.hpp"
#include "tools/concepts.hpp"

#include "tools/Log.hpp"

#include "tools/std20/type_traits.hpp"
33 changes: 33 additions & 0 deletions src/tools/std20/type_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef NJOY_TOOLS_STD20_TYPE_TRAITS
#define NJOY_TOOLS_STD20_TYPE_TRAITS

// system includes
#include <type_traits>

// other includes

namespace njoy {
namespace tools {
namespace std20 {

template < typename T >
struct remove_cvref {
using type = std::remove_cv_t< std::remove_reference_t< T > >;
};

template < typename T >
using remove_cvref_t = typename remove_cvref< T >::type;

template < typename T >
struct type_identity {
using type = T;
};

template < typename T >
using type_identity_t = typename type_identity< T >::type;

} // std20 namespace
} // tools namespace
} // njoy namespace

#endif
2 changes: 2 additions & 0 deletions src/tools/std20/type_traits/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_cpp_test( std20.type_traits.remove_cvref remove_cvref.test.cpp )
add_cpp_test( std20.type_traits.type_identity type_identity.test.cpp )
31 changes: 31 additions & 0 deletions src/tools/std20/type_traits/test/remove_cvref.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// include Catch2
#include <catch2/catch_test_macros.hpp>

// what we are testing
#include "tools/std20/type_traits.hpp"

// other includes

// convenience typedefs
using namespace njoy::tools;

SCENARIO( "remove_cvref and remove_cvref_t" ) {

CHECK( std::is_same_v< std20::remove_cvref< int >::type, int > );
CHECK( std::is_same_v< std20::remove_cvref< int& >::type, int > );
CHECK( std::is_same_v< std20::remove_cvref< int&& >::type, int > );
CHECK( std::is_same_v< std20::remove_cvref< const int& >::type, int > );
CHECK( std::is_same_v< std20::remove_cvref< int[2] >::type, int[2] > );
CHECK( std::is_same_v< std20::remove_cvref< const int[2] >::type, int[2] > );
CHECK( std::is_same_v< std20::remove_cvref< const int(&)[2] >::type, int[2] > );
CHECK( std::is_same_v< std20::remove_cvref< int(int) >::type, int(int) > );

CHECK( std::is_same_v< std20::remove_cvref_t< int >, int > );
CHECK( std::is_same_v< std20::remove_cvref_t< int& >, int > );
CHECK( std::is_same_v< std20::remove_cvref_t< int&& >, int > );
CHECK( std::is_same_v< std20::remove_cvref_t< const int& >, int > );
CHECK( std::is_same_v< std20::remove_cvref_t< int[2] >, int[2] > );
CHECK( std::is_same_v< std20::remove_cvref_t< const int[2] >, int[2] > );
CHECK( std::is_same_v< std20::remove_cvref_t< const int(&)[2] >, int[2] > );
CHECK( std::is_same_v< std20::remove_cvref_t< int(int) >, int(int) > );
} // SCENARIO
31 changes: 31 additions & 0 deletions src/tools/std20/type_traits/test/type_identity.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// include Catch2
#include <catch2/catch_test_macros.hpp>

// what we are testing
#include "tools/std20/type_traits.hpp"

// other includes

// convenience typedefs
using namespace njoy::tools;

SCENARIO( "type_identity and type_identity_t" ) {

CHECK( std::is_same_v< std20::type_identity< int >::type, int > );
CHECK( std::is_same_v< std20::type_identity< int& >::type, int& > );
CHECK( std::is_same_v< std20::type_identity< int&& >::type, int&& > );
CHECK( std::is_same_v< std20::type_identity< const int& >::type, const int& > );
CHECK( std::is_same_v< std20::type_identity< int[2] >::type, int[2] > );
CHECK( std::is_same_v< std20::type_identity< const int[2] >::type, const int[2] > );
CHECK( std::is_same_v< std20::type_identity< const int(&)[2] >::type, const int(&)[2] > );
CHECK( std::is_same_v< std20::type_identity< int(int) >::type, int(int) > );

CHECK( std::is_same_v< std20::type_identity_t< int >, int > );
CHECK( std::is_same_v< std20::type_identity_t< int& >, int& > );
CHECK( std::is_same_v< std20::type_identity_t< int&& >, int&& > );
CHECK( std::is_same_v< std20::type_identity_t< const int& >, const int& > );
CHECK( std::is_same_v< std20::type_identity_t< int[2] >, int[2] > );
CHECK( std::is_same_v< std20::type_identity_t< const int[2] >, const int[2] > );
CHECK( std::is_same_v< std20::type_identity_t< const int(&)[2] >, const int(&)[2] > );
CHECK( std::is_same_v< std20::type_identity_t< int(int) >, int(int) > );
} // SCENARIO

0 comments on commit e5ee470

Please sign in to comment.