Guest User

Untitled

a guest
Jun 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <vector>
  2. #include <utility>
  3. #include <stdio.h>
  4.  
  5. template<typename T>
  6. class myvector : public std::vector<T> {};
  7.  
  8. template<typename T>
  9. struct check_for
  10. {
  11. template<typename U>
  12. static constexpr bool _data(U*, decltype(std::declval<U>().data())* = nullptr) { return true; }
  13. template<typename U>
  14. static constexpr bool _data(...) { return false; }
  15.  
  16. template<typename U>
  17. static constexpr bool _get_allocator(U*, decltype(std::declval<U>().get_allocator())* = nullptr) { return true; }
  18. template<typename U>
  19. static constexpr bool _get_allocator(...) { return false; }
  20.  
  21. static constexpr bool data = _data<T>(nullptr);
  22. static constexpr bool get_allocator = _get_allocator<T>(nullptr);
  23. };
  24.  
  25. int main(int argc, char** argv)
  26. {
  27. printf("Check type 'bool' for function 'data': %d\n", check_for<bool>::data);
  28. printf("Check type 'std::vector<int>' for function 'data': %d\n", check_for<std::vector<int>>::data);
  29. printf("Check type 'myvector<int>' for function 'data': %d\n", check_for<myvector<int>>::data);
  30.  
  31. printf("Check type 'bool' for function 'get_allocator': %d\n", check_for<bool>::get_allocator);
  32. printf("Check type 'std::vector<int>' for function 'get_allocator': %d\n", check_for<std::vector<int>>::get_allocator);
  33. printf("Check type 'myvector<int>' for function 'get_allocator': %d\n", check_for<myvector<int>>::get_allocator);
  34.  
  35. decltype(std::declval<std::vector<int>>().get_allocator()) a;
  36.  
  37. int* b = a.allocate(10);
  38. printf("%p\n", b);
  39. a.deallocate(b, 10);
  40.  
  41. return 0;
  42. }
Add Comment
Please, Sign In to add comment