Guest User

Untitled

a guest
May 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. template <int N>
  2. struct Factorial
  3. {
  4. enum { value = N * Factorial<N - 1>::value };
  5. };
  6.  
  7. template <>
  8. struct Factorial<0>
  9. {
  10. enum { value = 1 };
  11. };
  12.  
  13. // Factorial<4>::value == 24
  14. // Factorial<0>::value == 1
  15. void foo()
  16. {
  17. int x = Factorial<4>::value; // == 24
  18. int y = Factorial<0>::value; // == 1
  19. }
  20.  
  21. template <unsigned target, unsigned mask>
  22. struct _mask_merger
  23. {
  24. enum
  25. {
  26. ROW0 = ((target >> (((mask >> 0) & 3) << 1)) & 3) << 0,
  27. ROW1 = ((target >> (((mask >> 2) & 3) << 1)) & 3) << 2,
  28. ROW2 = ((target >> (((mask >> 4) & 3) << 1)) & 3) << 4,
  29. ROW3 = ((target >> (((mask >> 6) & 3) << 1)) & 3) << 6,
  30.  
  31. MASK = ROW0 | ROW1 | ROW2 | ROW3,
  32. };
  33. };
  34.  
  35. template <typename T>
  36. struct value_type {
  37. typedef typename T::value_type type;
  38. };
  39.  
  40. template <typename T>
  41. struct value_type<T*> {
  42. typedef T type;
  43. };
  44.  
  45. template< unsigned long long N >
  46. struct binary
  47. {
  48. enum { value = (N % 10) + 2 * binary< N / 10 > :: value } ;
  49. };
  50. template<>
  51. struct binary< 0 >
  52. {
  53. enum { value = 0 } ;
  54. };
  55.  
  56. double innerLoop(const bool b, const vector<double> & v)
  57. {
  58. // some logic involving b
  59.  
  60. for (vector::const_iterator it = v.begin; it != v.end(); ++it)
  61. {
  62. // significant logic involving b
  63. }
  64.  
  65. // more logic involving b
  66. return ....
  67. }
  68.  
  69. template <bool b> double innerLoop_B(vector<double> v) { ... same as before ... }
  70. double innerLoop(const bool b, const vector<double> & v)
  71. { return b ? innerLoop_templ_B<true>(v) : innerLoop_templ_B<false>(v) ); }
  72.  
  73. typedef
  74. type_list_generator< signed char
  75. , signed short
  76. , signed int
  77. , signed long
  78. >::result_type
  79. signed_int_type_list;
  80.  
  81. typedef
  82. type_list_find_if< signed_int_type_list
  83. , exact_size_predicate<8>
  84. >::result_type
  85. int8_t;
  86.  
  87. typedef
  88. type_list_find_if< signed_int_type_list
  89. , exact_size_predicate<16>
  90. >::result_type
  91. int16_t;
  92.  
  93. typedef
  94. type_list_find_if< signed_int_type_list
  95. , exact_size_predicate<32>
  96. >::result_type
  97. int32_t;
  98.  
  99. template< typename TFunc, typename TFwdIter >
  100. typename func_traits<TFunc>::result_t callFunc(TFunc f, TFwdIter begin, TFwdIter end);
Add Comment
Please, Sign In to add comment