Advertisement
Dukales

copy const volatile reference qualifiers

Jun 20th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. template< std::size_t cvref_id, typename type > struct add_cvref;
  2. template< typename to > struct add_cvref<  0, to > { using type =          to         ; };
  3. template< typename to > struct add_cvref<  1, to > { using type =          to const   ; };
  4. template< typename to > struct add_cvref<  2, to > { using type =          to       & ; };
  5. template< typename to > struct add_cvref<  3, to > { using type =          to const & ; };
  6. template< typename to > struct add_cvref<  4, to > { using type =          to       &&; };
  7. template< typename to > struct add_cvref<  5, to > { using type =          to const &&; };
  8. template< typename to > struct add_cvref<  6, to > { using type = volatile to         ; };
  9. template< typename to > struct add_cvref<  7, to > { using type = volatile to const   ; };
  10. template< typename to > struct add_cvref<  8, to > { using type = volatile to       & ; };
  11. template< typename to > struct add_cvref<  9, to > { using type = volatile to const & ; };
  12. template< typename to > struct add_cvref< 10, to > { using type = volatile to       &&; };
  13. template< typename to > struct add_cvref< 11, to > { using type = volatile to const &&; };
  14.  
  15. template< std::size_t cvref_id, typename to >
  16. using add_cvref_t = typename add_cvref< cvref_id, to >::type;
  17.  
  18. template< typename type > constexpr std::size_t cvref_id                           =  0;
  19. template< typename type > constexpr std::size_t cvref_id<          type const    > =  1;
  20. template< typename type > constexpr std::size_t cvref_id<          type       &  > =  2;
  21. template< typename type > constexpr std::size_t cvref_id<          type const &  > =  3;
  22. template< typename type > constexpr std::size_t cvref_id<          type       && > =  4;
  23. template< typename type > constexpr std::size_t cvref_id<          type const && > =  5;
  24. template< typename type > constexpr std::size_t cvref_id< volatile type          > =  6;
  25. template< typename type > constexpr std::size_t cvref_id< volatile type const    > =  7;
  26. template< typename type > constexpr std::size_t cvref_id< volatile type       &  > =  8;
  27. template< typename type > constexpr std::size_t cvref_id< volatile type const &  > =  9;
  28. template< typename type > constexpr std::size_t cvref_id< volatile type       && > = 10;
  29. template< typename type > constexpr std::size_t cvref_id< volatile type const && > = 11;
  30.  
  31. template< typename from, typename to >
  32. using copy_cv_reference_t = add_cvref_t< cvref_id< from >, to >;
  33.  
  34. namespace test
  35. {
  36.  
  37. struct A {};
  38. struct B {};
  39. static_assert(std::is_same< copy_cv_reference_t<          A         , B >,          B          >{});
  40. static_assert(std::is_same< copy_cv_reference_t<          A const   , B >,          B const    >{});
  41. static_assert(std::is_same< copy_cv_reference_t< volatile A         , B >, volatile B          >{});
  42. static_assert(std::is_same< copy_cv_reference_t< volatile A const   , B >, volatile B const    >{});
  43. static_assert(std::is_same< copy_cv_reference_t<          A        &, B >,          B        & >{});
  44. static_assert(std::is_same< copy_cv_reference_t<          A const  &, B >,          B const  & >{});
  45. static_assert(std::is_same< copy_cv_reference_t< volatile A        &, B >, volatile B        & >{});
  46. static_assert(std::is_same< copy_cv_reference_t< volatile A const  &, B >, volatile B const  & >{});
  47. static_assert(std::is_same< copy_cv_reference_t<          A       &&, B >,          B       && >{});
  48. static_assert(std::is_same< copy_cv_reference_t<          A const &&, B >,          B const && >{});
  49. static_assert(std::is_same< copy_cv_reference_t< volatile A       &&, B >, volatile B       && >{});
  50. static_assert(std::is_same< copy_cv_reference_t< volatile A const &&, B >, volatile B const && >{});
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement