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 9 // UNSUPPORTED: c++98, c++03, c++11, c++14 10 11 // template <class T> constexpr add_const<T>& as_const(T& t) noexcept; // C++17 12 // template <class T> add_const<T>& as_const(const T&&) = delete; // C++17 13 14 #include <utility> 15 #include <cassert> 16 17 #include "test_macros.h" 18 19 struct S {int i;}; 20 bool operator==(const S& x, const S& y) { return x.i == y.i; } 21 bool operator==(const volatile S& x, const volatile S& y) { return x.i == y.i; } 22 23 template<typename T> 24 void test(T& t) 25 { 26 static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const (t))>::type>::value, ""); 27 static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const< T>(t))>::type>::value, ""); 28 static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<const T>(t))>::type>::value, ""); 29 static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<volatile T>(t))>::type>::value, ""); 30 static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<const volatile T>(t))>::type>::value, ""); 31 32 assert(std::as_const(t) == t); 33 assert(std::as_const< T>(t) == t); 34 assert(std::as_const<const T>(t) == t); 35 assert(std::as_const<volatile T>(t) == t); 36 assert(std::as_const<const volatile T>(t) == t); 37 } 38 39 int main(int, char**) 40 { 41 int i = 3; 42 double d = 4.0; 43 S s{2}; 44 test(i); 45 test(d); 46 test(s); 47 48 return 0; 49 } 50