gpt4 book ai didi

c++ - 检查两种类型是否具有可比性

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:51 26 4
gpt4 key购买 nike

我有以下代码来确定 2 种类型是否具有可比性。

template<typename T, typename U, typename = std::void_t<>>
struct is_comparable
: std::false_type
{};

template<typename T, typename U>
struct is_comparable<T, U, std::void_t<decltype((std::declval<T>() == std::declval<U>()))>>
: std::true_type
{};

这是实现我想要做的事情的可接受方式吗?你能看出这个设计有什么问题吗?

编辑

牢记 cdhowie 的评论和 Henri Menki 的回答,这就是代码现在的样子。

namespace meta
{
template<typename T, typename U, typename = std::void_t<>>
struct has_equal_to_operator
: std::false_type
{};

template<typename R, typename T, typename U, typename = std::void_t<>>
struct has_equal_to_operator_r
: std::false_type
{};

template<typename T, typename U, typename = std::void_t<>>
struct has_nothrow_equal_to_operator
: std::false_type
{};

template<typename R, typename T, typename U, typename = std::void_t<>>
struct has_nothrow_equal_to_operator_r
: std::false_type
{};

template<typename T, typename U>
struct has_equal_to_operator<T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
: std::true_type
{};

template<typename R, typename T, typename U>
struct has_equal_to_operator_r<R, T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
: std::is_convertible<decltype(std::declval<T>() == std::declval<U>()), R>
{};

template<typename T, typename U>
struct has_nothrow_equal_to_operator<T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
: std::bool_constant<noexcept(std::declval<T>() == std::declval<U>())>
{};

template<typename R, typename T, typename U>
struct has_nothrow_equal_to_operator_r<R, T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
: std::bool_constant<(noexcept(std::declval<T>() == std::declval<U>()) && std::is_convertible_v<decltype(std::declval<T>() == std::declval<U>()), R>)>
{};

template<typename T, typename U>
inline constexpr auto has_equal_to_operator_v = has_equal_to_operator<T, U>::value;

template<typename R, typename T, typename U>
inline constexpr auto has_equal_to_operator_r_v = has_equal_to_operator_r<R, T, U>::value;

template<typename T, typename U>
inline constexpr auto has_nothrow_equal_to_operator_v = has_nothrow_equal_to_operator<T, U>::value;

template<typename R, typename T, typename U>
inline constexpr auto has_nothrow_equal_to_operator_r_v = has_nothrow_equal_to_operator_r<R, T, U>::value;
}

最佳答案

这是一个像问题中一样带有 void_t 的解决方案。此外,我会检查比较是否产生正确的类型(在本例中为 bool)。

#include <type_traits>
#include <iostream>

template < typename T, typename U >
using equality_comparison_t = decltype(std::declval<T&>() == std::declval<U&>());

template < typename T, typename U, typename = std::void_t<> >
struct is_equality_comparable
: std::false_type
{};

template < typename T, typename U >
struct is_equality_comparable < T, U, std::void_t< equality_comparison_t<T,U> > >
: std::is_same< equality_comparison_t<T,U>, bool >
{};

struct X {};

struct Y { int operator==(Y const&) { return 1; } };

int main()
{
static_assert(false == is_equality_comparable<X, X>(), "!");
static_assert( true == is_equality_comparable<std::string, std::string>(), "!!");
static_assert(false == is_equality_comparable<int, std::string>(), "!!!");
static_assert( true == is_equality_comparable<int, int>(), "!!!!");
static_assert(false == is_equality_comparable<Y, Y>(), "!!!!!");
}

关于c++ - 检查两种类型是否具有可比性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44531052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com