Add traits for checking whether types are comparable

This commit is contained in:
Martin Hořeňovský
2022-11-01 08:48:23 +01:00
parent b3dbd83da2
commit d7f8c36e4c
7 changed files with 126 additions and 0 deletions

View File

@@ -61,6 +61,7 @@ set(INTERNAL_HEADERS
${SOURCES_DIR}/internal/catch_clara.hpp
${SOURCES_DIR}/internal/catch_commandline.hpp
${SOURCES_DIR}/internal/catch_source_line_info.hpp
${SOURCES_DIR}/internal/catch_compare_traits.hpp
${SOURCES_DIR}/internal/catch_compiler_capabilities.hpp
${SOURCES_DIR}/catch_config.hpp
${SOURCES_DIR}/internal/catch_config_android_logwrite.hpp

View File

@@ -50,6 +50,7 @@
#include <catch2/internal/catch_case_sensitive.hpp>
#include <catch2/internal/catch_clara.hpp>
#include <catch2/internal/catch_commandline.hpp>
#include <catch2/internal/catch_compare_traits.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_config_android_logwrite.hpp>
#include <catch2/internal/catch_config_counter.hpp>

View File

@@ -0,0 +1,47 @@
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_COMPARE_TRAITS_HPP_INCLUDED
#define CATCH_COMPARE_TRAITS_HPP_INCLUDED
#include <catch2/internal/catch_void_type.hpp>
#include <type_traits>
namespace Catch {
namespace Detail {
#define CATCH_DEFINE_COMPARABLE_TRAIT( id, op ) \
template <typename, typename, typename = void> \
struct is_##id##_comparable : std::false_type {}; \
template <typename T, typename U> \
struct is_##id##_comparable< \
T, \
U, \
void_t<decltype( std::declval<T>() op std::declval<U>() )>> \
: std::true_type {}; \
template <typename, typename = void> \
struct is_##id##_0_comparable : std::false_type {}; \
template <typename T> \
struct is_##id##_0_comparable<T, \
void_t<decltype( std::declval<T>() op 0 )>> \
: std::true_type {};
// We need all 6 pre-spaceship comparison ops: <, <=, >, >=, ==, !=
CATCH_DEFINE_COMPARABLE_TRAIT( lt, < )
CATCH_DEFINE_COMPARABLE_TRAIT( le, <= )
CATCH_DEFINE_COMPARABLE_TRAIT( gt, > )
CATCH_DEFINE_COMPARABLE_TRAIT( ge, >= )
CATCH_DEFINE_COMPARABLE_TRAIT( eq, == )
CATCH_DEFINE_COMPARABLE_TRAIT( ne, != )
#undef CATCH_DEFINE_COMPARABLE_TRAIT
} // namespace Detail
} // namespace Catch
#endif // CATCH_COMPARE_TRAITS_HPP_INCLUDED

View File

@@ -72,6 +72,7 @@ internal_headers = [
'internal/catch_case_sensitive.hpp',
'internal/catch_clara.hpp',
'internal/catch_commandline.hpp',
'internal/catch_compare_traits.hpp',
'internal/catch_compiler_capabilities.hpp',
'internal/catch_config_android_logwrite.hpp',
'internal/catch_config_counter.hpp',