Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <utility>
- #include <stdio.h>
- template<typename T>
- class myvector : public std::vector<T> {};
- template<typename T>
- struct check_for
- {
- template<typename U>
- static constexpr bool _data(U*, decltype(std::declval<U>().data())* = nullptr) { return true; }
- template<typename U>
- static constexpr bool _data(...) { return false; }
- template<typename U>
- static constexpr bool _get_allocator(U*, decltype(std::declval<U>().get_allocator())* = nullptr) { return true; }
- template<typename U>
- static constexpr bool _get_allocator(...) { return false; }
- static constexpr bool data = _data<T>(nullptr);
- static constexpr bool get_allocator = _get_allocator<T>(nullptr);
- };
- int main(int argc, char** argv)
- {
- printf("Check type 'bool' for function 'data': %d\n", check_for<bool>::data);
- printf("Check type 'std::vector<int>' for function 'data': %d\n", check_for<std::vector<int>>::data);
- printf("Check type 'myvector<int>' for function 'data': %d\n", check_for<myvector<int>>::data);
- printf("Check type 'bool' for function 'get_allocator': %d\n", check_for<bool>::get_allocator);
- printf("Check type 'std::vector<int>' for function 'get_allocator': %d\n", check_for<std::vector<int>>::get_allocator);
- printf("Check type 'myvector<int>' for function 'get_allocator': %d\n", check_for<myvector<int>>::get_allocator);
- decltype(std::declval<std::vector<int>>().get_allocator()) a;
- int* b = a.allocate(10);
- printf("%p\n", b);
- a.deallocate(b, 10);
- return 0;
- }
Add Comment
Please, Sign In to add comment