c – 检查某个类是否具有某个成员函数的其他方法

前端之家收集整理的这篇文章主要介绍了c – 检查某个类是否具有某个成员函数的其他方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们来看看是否
  1. struct Thing {
  2. int foo(double,bool) {return 0;}
  3. };

在编译期间有int foo(double,bool)成员函数.有很多方法可以做到这一点,大多数只是其他方式的变体.有人会想到一种与我在这里提到的5种方式截然不同(或至少相当有创意)的方式吗?我只是想学习一些模板和SFINAE的新技术.

  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. // Using void_t (this includes using std::is_detected).
  5. template <typename T>
  6. using void_t = void;
  7.  
  8. template <typename T,typename = void>
  9. struct has_foo : std::false_type {};
  10.  
  11. template <typename T>
  12. struct has_foo<T,void_t<decltype(static_cast<int>(std::declval<T>().foo(double{},bool{})))>
  13. > : std::true_type {};
  14.  
  15. // Using the ... default argument.
  16. template <typename T>
  17. struct hasfoo {
  18. template <typename U>
  19. static std::true_type test (decltype(static_cast<int(T::*)(double,bool)>(&T::foo))*); // or 'decltype(static_cast<int>(std::declval<U>().foo(double{},bool{})))*' works fine too.
  20.  
  21. template <typename>
  22. static std::false_type test (...);
  23.  
  24. static constexpr bool value = decltype(test<T>(nullptr))::value;
  25. };
  26.  
  27. // Overloads and trailing return types.
  28. template <typename>
  29. struct Helper : std::true_type {};
  30.  
  31. template <typename T>
  32. auto helper(int) -> Helper<decltype(static_cast<int>(std::declval<T>().foo(double{},bool{})))>;
  33.  
  34. template <typename>
  35. std::false_type helper(long);
  36.  
  37. template <typename T>
  38. constexpr bool hasFoo() {return decltype(helper<T>(0))::value;}
  39.  
  40. // Comma operator (basically the same as the above).
  41. template <typename T>
  42. auto check(int) -> decltype(static_cast<int>(std::declval<T>().foo(double{},bool{})),std::true_type{});
  43.  
  44. template <typename T>
  45. std::false_type check(...);
  46.  
  47. template <typename T>
  48. using HasFoo = decltype(check<T>(0));
  49.  
  50. // Member function pointer template parameter.
  51. template <typename T>
  52. struct Hasfoo {
  53. template <typename U,int(U::*)(double,bool)>
  54. struct Tag;
  55.  
  56. template <typename U>
  57. static constexpr bool test (Tag<U,&U::foo>*) {return true;}
  58.  
  59. template <typename>
  60. static constexpr bool test (...) {return false;}
  61.  
  62. static constexpr bool value = test<T>(nullptr);
  63. };
  64.  
  65. // Tests
  66. struct Thing {
  67. int foo(double,bool) {return 0;}
  68. };
  69.  
  70. int main() {
  71. static_assert (has_foo<Thing>::value,"");
  72. static_assert (hasfoo<Thing>::value,"");
  73. static_assert (hasFoo<Thing>(),"");
  74. static_assert (HasFoo<Thing>::value,"");
  75. }

编辑:我刚刚想起了一个优雅而更通用的解决方案,Yakk很久以前就提出了一个不同的问题(这是他的实际打字,只是为了匹配foo函数修改):

  1. namespace Meta {
  2. namespace details {
  3. template<template<class...>class Z,class=void,class...Ts>
  4. struct can_apply : std::false_type {};
  5. template<template<class...>class Z,class...Ts>
  6. struct can_apply<Z,decltype((void)(std::declval<Z<Ts...>>())),Ts...>:
  7. std::true_type
  8. {};
  9. }
  10. template<template<class...>class Z,class...Ts>
  11. using can_apply = details::can_apply<Z,void,Ts...>;
  12. }
  13.  
  14. template<class T>
  15. using member_foo = decltype(static_cast<int(T::*)(double,bool)>(&T::foo));
  16.  
  17. template<class T>
  18. using has_member_foo = Meta::can_apply<member_foo,T>;

解决方法

Can someone think of a way that is vastly different (or at least fairly creative) than the 5 ways I mention here?

一个相当有创意的方法可能是下面的那个.
它基于noexcept运算符和一个简单的使用声明(此处称为Type).
SFINAE和部分模板专业化完成剩下的工作.

它遵循一个最小的工作示例:

  1. #include<type_traits>
  2. #include<utility>
  3.  
  4. template<typename T,bool>
  5. using Type = T;
  6.  
  7. template<typename T,typename = T>
  8. struct U: std::false_type {};
  9.  
  10. template<typename T>
  11. struct U<T,Type<T,noexcept(std::declval<T>().f(42))>>: std::true_type {};
  12.  
  13. struct S { void f(int) {} };
  14. struct T {};
  15.  
  16. int main() {
  17. static_assert(U<S>::value,"!");
  18. static_assert(not U<T>::value,"!");
  19. }

如果与其他解决方案相比,该解决方案存在问题.
作为示例,下面的结构也将通过测试:

  1. struct R { int f(double) {} };

换句话说,只要要测试的函数接受一个可以转换为42的类型的参数,并且无论返回类型是什么,它都被接受.

猜你在找的C&C++相关文章