pointer_to.pass.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <memory>
  9. // template <class Ptr>
  10. // struct pointer_traits
  11. // {
  12. // static pointer pointer_to(<details>);
  13. // ...
  14. // };
  15. #include <memory>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. template <class T>
  19. struct A
  20. {
  21. private:
  22. struct nat {};
  23. public:
  24. typedef T element_type;
  25. element_type* t_;
  26. A(element_type* t) : t_(t) {}
  27. static A pointer_to(typename std::conditional<std::is_void<element_type>::value,
  28. nat, element_type>::type& et)
  29. {return A(&et);}
  30. };
  31. int main(int, char**)
  32. {
  33. {
  34. int i = 0;
  35. static_assert((std::is_same<A<int>, decltype(std::pointer_traits<A<int> >::pointer_to(i))>::value), "");
  36. A<int> a = std::pointer_traits<A<int> >::pointer_to(i);
  37. assert(a.t_ == &i);
  38. }
  39. {
  40. (std::pointer_traits<A<void> >::element_type)0;
  41. }
  42. return 0;
  43. }