Advertisement
ulysses4ever

is_belonged

Feb 11th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <assert.h>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. template<bool Test, typename T = void>
  6. using enabled_if = typename enable_if<Test, T>::type;
  7.  
  8. template<typename ThisType, typename... Types>
  9. struct is_belong;
  10.  
  11. template<typename ThisType, typename ThatType, typename... Types>
  12. struct is_belong
  13.     <ThisType,
  14.      ThatType,
  15.      Types...> : is_belong<ThisType, Types...> {};
  16.  
  17. template<typename ThisType, typename... Types>
  18. struct is_belong
  19.     <ThisType,
  20.      ThisType,
  21.      Types...> : true_type {};
  22.  
  23. template<typename ThisType>
  24. struct is_belong<ThisType> : false_type {};
  25.  
  26. template<typename ThisType, typename... Types>  // ДОБАВЛЕНО:
  27. constexpr bool is_belonged() {return is_belong<ThisType, Types...>::value;}
  28.  
  29. //Identity function which works only
  30. //for `short`, `int` and `long`:
  31. template<typename T,
  32.      enabled_if<is_belonged<T, short, int, long>()>... Check> // ИЗМЕНЕНО
  33.     T I(const T& val) {
  34.         return val;
  35.     }      
  36.  
  37. int main() {
  38.    
  39.     int n = 100;
  40.     int n1 = I(n);
  41.     assert(n == n1);
  42.  
  43.     double d = 100;
  44.     //Compilation error - works fine
  45.     double d1 = I(d);
  46.     //assert(d == d1);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement