Home | History | Annotate | Download | only in as_const
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03, c++11, c++14
     11 
     12 // template <class T> constexpr add_const<T>& as_const(T& t) noexcept;      // C++17
     13 // template <class T>           add_const<T>& as_const(const T&&) = delete; // C++17
     14 
     15 #include <utility>
     16 #include <cassert>
     17 
     18 struct S {int i;};
     19 bool operator==(const S& x, const S& y) { return x.i == y.i; }
     20 bool operator==(const volatile S& x, const volatile S& y) { return x.i == y.i; }
     21 
     22 template<typename T>
     23 void test(T& t)
     24 {
     25     static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const                  (t))>::type>::value, "");
     26     static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<               T>(t))>::type>::value, "");
     27     static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<const          T>(t))>::type>::value, "");
     28     static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<volatile       T>(t))>::type>::value, "");
     29     static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<const volatile T>(t))>::type>::value, "");
     30 
     31     assert(std::as_const(t) == t);
     32     assert(std::as_const<               T>(t) == t);
     33     assert(std::as_const<const          T>(t) == t);
     34     assert(std::as_const<volatile       T>(t) == t);
     35     assert(std::as_const<const volatile T>(t) == t);
     36 }
     37 
     38 int main()
     39 {
     40     int i = 3;
     41     double d = 4.0;
     42     S s{2};
     43     test(i);
     44     test(d);
     45     test(s);
     46 }
     47