fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include <boost/utility/enable_if.hpp>
  5. #include <boost/type_traits/is_same.hpp>
  6.  
  7. typedef unsigned int UINT;
  8.  
  9. //Foo.h
  10. template <typename T>
  11. struct returnThis {};
  12.  
  13. class Foo
  14. {
  15. public:
  16.  
  17. template <typename T>
  18. typename boost::enable_if<boost::is_same<T,float>,returnThis<float>*>::type
  19. bar();
  20.  
  21. template <typename T>
  22. typename boost::enable_if_c<boost::is_same<T,UINT>::value and boost::is_same<UINT,unsigned int>::value,
  23. returnThis<UINT>*>::type
  24. bar();
  25.  
  26. template <typename T>
  27. typename boost::enable_if_c<boost::is_same<T,UINT>::value and not boost::is_same<UINT,unsigned int>::value,
  28. returnThis<UINT>*>::type
  29. bar();
  30. };
  31.  
  32.  
  33. int main() {
  34. Foo f;
  35. f.bar<float>();
  36. f.bar<UINT>();
  37. return 0;
  38. }
  39.  
  40.  
  41. // cppfile
  42. template <typename T>
  43. typename boost::enable_if<boost::is_same<T,float>,returnThis<float>*>::type
  44. Foo::bar()
  45. {
  46. cout << "bar<T==float>()\n";
  47. return 0;
  48. }
  49.  
  50. template <typename T>
  51. typename boost::enable_if_c<boost::is_same<T,UINT>::value and boost::is_same<UINT,unsigned int>::value,
  52. returnThis<UINT>*>::type
  53. Foo::bar()
  54. {
  55. cout << "bar<T==UINT and UINT==usigned int>()\n";
  56. return 0;
  57. }
  58.  
  59. template <typename T>
  60. typename boost::enable_if_c<boost::is_same<T,UINT>::value and not boost::is_same<UINT,unsigned int>::value,
  61. returnThis<UINT>*>::type
  62. Foo::bar()
  63. {
  64. cout << "bar<T==UINT and UINT!=usigned int>()\n";
  65. return 0;
  66. }
  67.  
  68.  
  69. template returnThis<float>* Foo::bar<float>();
  70. template returnThis<UINT>* Foo::bar<UINT>();
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
bar<T==float>()
bar<T==UINT and UINT==usigned int>()