Implement P0007: Constant View: A proposal for a std::as_const helper function template.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@253274 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -52,6 +52,9 @@ template <class T>
|
|||||||
>::type
|
>::type
|
||||||
move_if_noexcept(T& x) noexcept; // constexpr in C++14
|
move_if_noexcept(T& x) noexcept; // constexpr in C++14
|
||||||
|
|
||||||
|
template <class T> constexpr add_const<T>_t& as_const(T& t) noexcept; // C++17
|
||||||
|
template <class T> void as_const(const T&&) = delete; // C++17
|
||||||
|
|
||||||
template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
|
template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
@@ -242,6 +245,11 @@ move_if_noexcept(_Tp& __x) _NOEXCEPT
|
|||||||
return _VSTD::move(__x);
|
return _VSTD::move(__x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if _LIBCPP_STD_VER > 14
|
||||||
|
template <class _Tp> constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
|
||||||
|
template <class _Tp> void as_const(const _Tp&&) = delete;
|
||||||
|
#endif
|
||||||
|
|
||||||
struct _LIBCPP_TYPE_VIS_ONLY piecewise_construct_t { };
|
struct _LIBCPP_TYPE_VIS_ONLY piecewise_construct_t { };
|
||||||
#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY)
|
#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY)
|
||||||
extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
|
extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
|
||||||
|
|||||||
22
test/std/utilities/utility/as_const/as_const.fail.cpp
Normal file
22
test/std/utilities/utility/as_const/as_const.fail.cpp
Normal file
@@ -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 <class T> constexpr add_const<T>& as_const(T& t) noexcept; // C++17
|
||||||
|
// template <class T> add_const<T>& as_const(const T&&) = delete; // C++17
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
struct S {int i;};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::as_const(S{});
|
||||||
|
}
|
||||||
46
test/std/utilities/utility/as_const/as_const.pass.cpp
Normal file
46
test/std/utilities/utility/as_const/as_const.pass.cpp
Normal file
@@ -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 <class T> constexpr add_const<T>& as_const(T& t) noexcept; // C++17
|
||||||
|
// template <class T> add_const<T>& as_const(const T&&) = delete; // C++17
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
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<typename T>
|
||||||
|
void test(T& t)
|
||||||
|
{
|
||||||
|
static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const (t))>::type>::value, "");
|
||||||
|
static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const< T>(t))>::type>::value, "");
|
||||||
|
static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<const T>(t))>::type>::value, "");
|
||||||
|
static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<volatile T>(t))>::type>::value, "");
|
||||||
|
static_assert(std::is_const<typename std::remove_reference<decltype(std::as_const<const volatile T>(t))>::type>::value, "");
|
||||||
|
|
||||||
|
assert(std::as_const(t) == t);
|
||||||
|
assert(std::as_const< T>(t) == t);
|
||||||
|
assert(std::as_const<const T>(t) == t);
|
||||||
|
assert(std::as_const<volatile T>(t) == t);
|
||||||
|
assert(std::as_const<const volatile T>(t) == t);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i = 3;
|
||||||
|
double d = 4.0;
|
||||||
|
S s{2};
|
||||||
|
test(i);
|
||||||
|
test(d);
|
||||||
|
test(s);
|
||||||
|
}
|
||||||
@@ -73,7 +73,7 @@
|
|||||||
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0004R1.html">P0004R1</a></td><td>LWG</td><td>Remove Deprecated iostreams aliases.</td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0004R1.html">P0004R1</a></td><td>LWG</td><td>Remove Deprecated iostreams aliases.</td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
||||||
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0006R0.html">P0006R0</a></td><td>LWG</td><td>Adopt Type Traits Variable Templates for C++17.</td><td>Kona</td><td>In progress</td><td></td></tr>
|
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0006R0.html">P0006R0</a></td><td>LWG</td><td>Adopt Type Traits Variable Templates for C++17.</td><td>Kona</td><td>In progress</td><td></td></tr>
|
||||||
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0092R1.html">P0092R1</a></td><td>LWG</td><td>Polishing <chrono></td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0092R1.html">P0092R1</a></td><td>LWG</td><td>Polishing <chrono></td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
||||||
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0007R1.html">P0007R1</a></td><td>LWG</td><td>Constant View: A proposal for a <tt>std::as_const</tt> helper function template.</td><td>Kona</td><td>In progress</td><td></td></tr>
|
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0007R1.html">P0007R1</a></td><td>LWG</td><td>Constant View: A proposal for a <tt>std::as_const</tt> helper function template.</td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
||||||
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0156R0.htm" >P0156R0</a></td><td>LWG</td><td>Variadic lock_guard(rev 3).</td><td>Kona</td><td></td><td></td></tr>
|
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0156R0.htm" >P0156R0</a></td><td>LWG</td><td>Variadic lock_guard(rev 3).</td><td>Kona</td><td></td><td></td></tr>
|
||||||
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0074R0.html">P0074R0</a></td><td>LWG</td><td>Making <tt>std::owner_less</tt> more flexible</td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0074R0.html">P0074R0</a></td><td>LWG</td><td>Making <tt>std::owner_less</tt> more flexible</td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
||||||
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0013R1.html">P0013R1</a></td><td>LWG</td><td>Logical type traits rev 2</td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
<tr><td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/P0013R1.html">P0013R1</a></td><td>LWG</td><td>Logical type traits rev 2</td><td>Kona</td><td>Complete</td><td>3.8</td></tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user