Advertisement
bueddl

implement std::type_traits (C++11 and C++14)

Mar 2nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <string>
  2.  
  3. namespace op {
  4.  
  5. // ------------------------------------
  6.  
  7. template<typename T>
  8. struct remove_const {
  9.     typedef T type;
  10. };
  11.  
  12. template<typename T>
  13. struct remove_const<T const> {
  14.     typedef T type;
  15. };
  16.  
  17. template<typename T>
  18. using remove_const_t = typename remove_const<T>::type;
  19.  
  20. // ------------------------------------
  21.  
  22. template<typename T>
  23. struct remove_reference {
  24.     typedef T type;
  25. };
  26.  
  27. template<typename T>
  28. struct remove_reference<T&> {
  29.     typedef T type;
  30. };
  31.  
  32. template<typename T>
  33. struct remove_reference < T&& > {
  34.     typedef T type;
  35. };
  36.  
  37. template<typename T>
  38. using remove_reference_t = typename remove_reference<T>::type;
  39.  
  40. // ------------------------------------
  41.  
  42. }
  43.  
  44. int main()
  45. {
  46.     using T1 = op::remove_const<const std::string>::type;   // C++11
  47.     T1 str1 = "Das";
  48.     str1 += " ist ein Test!";
  49.  
  50.     using T2 = op::remove_reference<T1&>::type;             // C++11
  51.     T2 str2 = "No more ref now";
  52.  
  53.     using T3 = op::remove_const_t<const std::string>;       // C++14
  54.     T3 str3 = "A string";
  55.  
  56.     op::remove_reference_t<T3&> str4 = "some string";       // C++14
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement