检查类/结构是否有特定的操作符

Check if class/struct has a specific operator

本文关键字:操作符 是否 结构 检查      更新时间:2023-10-16

我需要特殊化我的模板之一,并希望根据可用的操作符做到这一点。

我需要知道我是否可以做Foo == Bar

template<class T >
std::enable_if_t<has_equalOperator<T>::value> Compare( const T& other )
{
  // can compare using ==
  ...
} 
template<class T >
std::enable_if_t<!has_equalOperator<T>::value> Compare( const T& other )
{
  // cannot compare use some other method
  ...
} 

标准有类似的东西吗?如果不是,我该如何执行它?

这类特征类型几乎总是采用以下形式:

#include <utility>
#include <iostream>
#include <string>
template<class T, class Arg>
  struct has_equals_impl
  {
    template<class U> static 
      auto test(const U* p) 
      -> decltype( /* the test is here */
                  (*p) == std::declval<Arg>(), 
                   /* end of test */
                  void(), std::true_type());
    static auto test(...) -> std::false_type;
    using type = decltype(test((const T*)nullptr));
  };
// this typedef ensures that the actual type is either std::true_type or std::false_type
template<class T, class Arg> using has_equals = typename has_equals_impl<T, Arg>::type;
int main()
{
  // int == int? yes
  std::cout << has_equals<int, int>() << std::endl;
  // string == int? no
  std::cout << has_equals<std::string, int>() << std::endl;
}

这是一套几乎完整的二进制运算符检查器,带有一些测试。

你应该了解大意。注意,我必须创建left_shiftright_shift函数对象,因为它们在标准库中已经不存在了。

#include <utility>
#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>

template<class X, class Y, class Op>
struct op_valid_impl
{
    template<class U, class L, class R>
    static auto test(int) -> decltype(std::declval<U>()(std::declval<L>(), std::declval<R>()),
                                      void(), std::true_type());
    template<class U, class L, class R>
    static auto test(...) -> std::false_type;
    using type = decltype(test<Op, X, Y>(0));
};
template<class X, class Y, class Op> using op_valid = typename op_valid_impl<X, Y, Op>::type;
namespace notstd {
    struct left_shift {
        template <class L, class R>
        constexpr auto operator()(L&& l, R&& r) const
        noexcept(noexcept(std::forward<L>(l) << std::forward<R>(r)))
        -> decltype(std::forward<L>(l) << std::forward<R>(r))
        {
            return std::forward<L>(l) << std::forward<R>(r);
        }
    };
    struct right_shift {
        template <class L, class R>
        constexpr auto operator()(L&& l, R&& r) const
        noexcept(noexcept(std::forward<L>(l) >> std::forward<R>(r)))
        -> decltype(std::forward<L>(l) >> std::forward<R>(r))
        {
            return std::forward<L>(l) >> std::forward<R>(r);
        }
    };
}
template<class X, class Y> using has_equality = op_valid<X, Y, std::equal_to<>>;
template<class X, class Y> using has_inequality = op_valid<X, Y, std::not_equal_to<>>;
template<class X, class Y> using has_less_than = op_valid<X, Y, std::less<>>;
template<class X, class Y> using has_less_equal = op_valid<X, Y, std::less_equal<>>;
template<class X, class Y> using has_greater_than = op_valid<X, Y, std::greater<>>;
template<class X, class Y> using has_greater_equal = op_valid<X, Y, std::greater_equal<>>;
template<class X, class Y> using has_bit_xor = op_valid<X, Y, std::bit_xor<>>;
template<class X, class Y> using has_bit_or = op_valid<X, Y, std::bit_or<>>;
template<class X, class Y> using has_left_shift = op_valid<X, Y, notstd::left_shift>;
template<class X, class Y> using has_right_shift = op_valid<X, Y, notstd::right_shift>;
int main()
{
    assert(( has_equality<int, int>() ));
    assert((not has_equality<std::string&, int const&>()()));
    assert((has_equality<std::string&, std::string const&>()()));
    assert(( has_inequality<int, int>() ));
    assert(( has_less_than<int, int>() ));
    assert(( has_greater_than<int, int>() ));
    assert(( has_left_shift<std::ostream&, int>() ));
    assert(( has_left_shift<std::ostream&, int&>() ));
    assert(( has_left_shift<std::ostream&, int const&>() ));
    assert((not has_right_shift<std::istream&, int>()()));
    assert((has_right_shift<std::istream&, int&>()()));
    assert((not has_right_shift<std::istream&, int const&>()()));
}

在c++ 17中,可以简化为std::is_detected:

typename <typename LHS, typename RHS>
using equal_t = decltype(std::declval<LHS>() == std::declval<RHS>());
template <typename T>
using has_equal = std::is_detected<equal_t, T, T>;
演示