Dukales

has_member type trait

Jul 30th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. #include <cstdlib>
  5.  
  6. namespace std
  7. {
  8.    
  9. template < typename ... > using void_t = void;
  10.  
  11. }
  12.  
  13. struct tag
  14. {
  15.    
  16.     std::size_t id_;
  17.    
  18. };
  19.  
  20. template< typename type, typename = void >
  21. struct is_tagged
  22.         : std::false_type
  23. {
  24.  
  25. };
  26.  
  27. template< typename type >
  28. struct is_tagged< type, std::void_t< decltype(type::tag_) > >
  29.         : std::is_same< decltype(type::tag_), tag >
  30. {
  31.  
  32. };
  33.  
  34. struct A {};
  35.  
  36. static_assert(!is_tagged< A >{});
  37.  
  38. struct B { tag tag_; };
  39.  
  40. static_assert(is_tagged< B >{});
  41.  
  42. struct C { int tag_; };
  43.  
  44. static_assert(!is_tagged< C >{});
  45.  
  46. int
  47. main()
  48. {
  49.    
  50.     return EXIT_SUCCESS;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment