fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. template< typename Base, typename... T >
  6. struct Comparer
  7. {
  8. template< T Base::* ... ptr >
  9. struct Members
  10. {
  11. static bool compare( Base const& lhs, Base const& rhs )
  12. {
  13. bool arr[]{ ( lhs.*ptr == rhs.*ptr )... };
  14. return all_of( begin(arr), end(arr), [](bool b){return b;} );
  15. }
  16. };
  17. };
  18.  
  19. template< typename T > struct get_type;
  20. template< typename Type, typename Class >
  21. struct get_type<Type Class::*>
  22. {
  23. using type = Type;
  24. using class_type = Class;
  25. };
  26.  
  27. template<typename...> struct type_list;
  28.  
  29. template<typename ... Args>
  30. type_list<Args...> get_types( Args... );
  31.  
  32. template<typename> struct create_comparer;
  33. template< typename First, typename ... Tail >
  34. struct create_comparer<type_list<First, Tail...>> :
  35. Comparer<typename get_type<First>::class_type,
  36. typename get_type<First>::type,
  37. typename get_type<Tail>::type...> {};
  38.  
  39. #define CREATE_COMPARER(...) create_comparer<decltype(get_types(__VA_ARGS__))>::Members<__VA_ARGS__>
  40.  
  41. struct A
  42. {
  43. int a;
  44. char b;
  45. };
  46.  
  47. int main()
  48. {
  49. A a{4, '7'}, b=a, c{5, '7'};
  50. cout << CREATE_COMPARER(&A::a, &A::b)::compare(a, b);
  51. cout << CREATE_COMPARER(&A::a, &A::b)::compare(a, c);
  52. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
10