fork download
  1. // -*- begin of type_info.hpp -*-
  2.  
  3. #ifndef _type_info_hpp_
  4. #define _type_info_hpp_
  5.  
  6. namespace rtti
  7. {
  8.  
  9. namespace detail
  10. {
  11.  
  12. struct identity {};
  13.  
  14. template < typename >
  15. struct type_info_impl
  16. { static identity impl_; };
  17.  
  18. template < typename T >
  19. identity type_info_impl< T >::impl_;
  20.  
  21. template < typename >
  22. struct type_forwarding {};
  23.  
  24. } // namespace detail
  25.  
  26. struct type_info_
  27. {
  28. private:
  29. const detail::identity *id_ptr_;
  30.  
  31. template < typename T >
  32. friend type_info_
  33. typeid_( void );
  34.  
  35. template < typename T >
  36. explicit inline
  37. type_info_( detail::type_forwarding< T > )
  38. : id_ptr_( &detail::type_info_impl< T >::impl_ ) {}
  39.  
  40. public:
  41. inline bool
  42. operator==( const type_info_ &ti ) const
  43. { return this->id_ptr_ == ti.id_ptr_; }
  44.  
  45. inline bool
  46. operator!=( const type_info_ &ti ) const
  47. { return !( *this == ti ); }
  48. };
  49.  
  50. template < typename T >
  51. inline type_info_
  52. typeid_( void )
  53. { return type_info_( detail::type_forwarding< T >() ); }
  54.  
  55. } // namespace rtti
  56.  
  57. #endif // _type_info_hpp_
  58.  
  59. // -*- end of type_info.hpp -*-
  60.  
  61.  
  62. // -*- begin of test.cpp -*-
  63. //#include "type_info.hpp"
  64.  
  65. namespace
  66. {
  67.  
  68. rtti::type_info_ int_info = rtti::typeid_< int >();
  69.  
  70. }
  71.  
  72. bool
  73. test( rtti::type_info_ ti )
  74. { return int_info == ti; }
  75.  
  76. // -*- end of test.cpp
  77.  
  78.  
  79. // -*- begin of main.cpp
  80.  
  81. #include <iostream>
  82.  
  83. //#include "type_info.hpp"
  84.  
  85. bool
  86. test( rtti::type_info_ );
  87.  
  88. template < typename T >
  89. void
  90. hoge( void )
  91. {
  92. std::cout << std::boolalpha
  93. << test( rtti::typeid_< T >() )
  94. << std::endl;
  95. }
  96.  
  97. int
  98. main( void )
  99. {
  100. std::cout << "int: ";
  101. hoge< int >();
  102.  
  103. std::cout << "float: ";
  104. hoge< float >();
  105. }
  106.  
  107. // -*- end of main.cpp -*-
  108.  
Success #stdin #stdout 0s 2724KB
stdin
Standard input is empty
stdout
int: true
float: false