diff --git a/include/utility b/include/utility index 8b982da7..29753206 100644 --- a/include/utility +++ b/include/utility @@ -52,6 +52,9 @@ template >::type move_if_noexcept(T& x) noexcept; // constexpr in C++14 +template constexpr add_const_t& as_const(T& t) noexcept; // C++17 +template void as_const(const T&&) = delete; // C++17 + template typename add_rvalue_reference::type declval() noexcept; template @@ -242,6 +245,11 @@ move_if_noexcept(_Tp& __x) _NOEXCEPT return _VSTD::move(__x); } +#if _LIBCPP_STD_VER > 14 +template constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; } +template void as_const(const _Tp&&) = delete; +#endif + struct _LIBCPP_TYPE_VIS_ONLY piecewise_construct_t { }; #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY) extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t(); diff --git a/test/std/utilities/utility/as_const/as_const.fail.cpp b/test/std/utilities/utility/as_const/as_const.fail.cpp new file mode 100644 index 00000000..6334e146 --- /dev/null +++ b/test/std/utilities/utility/as_const/as_const.fail.cpp @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// template constexpr add_const& as_const(T& t) noexcept; // C++17 +// template add_const& as_const(const T&&) = delete; // C++17 + +#include + +struct S {int i;}; + +int main() +{ + std::as_const(S{}); +} diff --git a/test/std/utilities/utility/as_const/as_const.pass.cpp b/test/std/utilities/utility/as_const/as_const.pass.cpp new file mode 100644 index 00000000..ff3f84a5 --- /dev/null +++ b/test/std/utilities/utility/as_const/as_const.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// template constexpr add_const& as_const(T& t) noexcept; // C++17 +// template add_const& as_const(const T&&) = delete; // C++17 + +#include +#include + +struct S {int i;}; +bool operator==(const S& x, const S& y) { return x.i == y.i; } +bool operator==(const volatile S& x, const volatile S& y) { return x.i == y.i; } + +template +void test(T& t) +{ + static_assert(std::is_const::type>::value, ""); + static_assert(std::is_const(t))>::type>::value, ""); + static_assert(std::is_const(t))>::type>::value, ""); + static_assert(std::is_const(t))>::type>::value, ""); + static_assert(std::is_const(t))>::type>::value, ""); + + assert(std::as_const(t) == t); + assert(std::as_const< T>(t) == t); + assert(std::as_const(t) == t); + assert(std::as_const(t) == t); + assert(std::as_const(t) == t); +} + +int main() +{ + int i = 3; + double d = 4.0; + S s{2}; + test(i); + test(d); + test(s); +} diff --git a/www/cxx1z_status.html b/www/cxx1z_status.html index 67a334f5..462c0bd1 100644 --- a/www/cxx1z_status.html +++ b/www/cxx1z_status.html @@ -73,7 +73,7 @@ P0004R1LWGRemove Deprecated iostreams aliases.KonaComplete3.8 P0006R0LWGAdopt Type Traits Variable Templates for C++17.KonaIn progress P0092R1LWGPolishing <chrono>KonaComplete3.8 - P0007R1LWGConstant View: A proposal for a std::as_const helper function template.KonaIn progress + P0007R1LWGConstant View: A proposal for a std::as_const helper function template.KonaComplete3.8 P0156R0LWGVariadic lock_guard(rev 3).Kona P0074R0LWGMaking std::owner_less more flexibleKonaComplete3.8 P0013R1LWGLogical type traits rev 2KonaComplete3.8