Commit 61ad58f3 by Jonathan Wakely Committed by Jonathan Wakely

PR libstdc++/79195 fix make_array type deduction

	PR libstdc++/79195
	* include/experimental/array (__make_array_elem): New class template
	and partial specialization.
	(__is_reference_wrapper): Move into __make_array_elem specialization.
	(make_array): Use __make_array_elem to determine element type and move
	static assertion into specialization. Qualify std::forward call.
	(to_array): Add exception specifiation.
	* testsuite/experimental/array/make_array.cc: Test argument types
	without a common type.
	* testsuite/experimental/array/neg.cc: Adjust expected error message.

From-SVN: r244813
parent f368600f
2017-01-23 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/79195
* include/experimental/array (__make_array_elem): New class template
and partial specialization.
(__is_reference_wrapper): Move into __make_array_elem specialization.
(make_array): Use __make_array_elem to determine element type and move
static assertion into specialization. Qualify std::forward call.
(to_array): Add exception specifiation.
* testsuite/experimental/array/make_array.cc: Test argument types
without a common type.
* testsuite/experimental/array/neg.cc: Adjust expected error message.
2017-01-22 Gerald Pfeifer <gerald@pfeifer.com> 2017-01-22 Gerald Pfeifer <gerald@pfeifer.com>
* doc/xml/manual/debug.xml: code.google.com uses https now. * doc/xml/manual/debug.xml: code.google.com uses https now.
......
...@@ -57,35 +57,40 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -57,35 +57,40 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @{ * @{
*/ */
template <typename _Up> template<typename _Dest, typename... _Types>
struct __is_reference_wrapper : false_type struct __make_array_elem
{}; {
using type = _Dest;
};
template<typename... _Types>
struct __make_array_elem<void, _Types...>
: common_type<_Types...>
{
template <typename>
struct __is_reference_wrapper : false_type
{};
template <typename _Up>
struct __is_reference_wrapper<reference_wrapper<_Up>> : true_type
{};
template <typename _Up> static_assert(!__or_<__is_reference_wrapper<decay_t<_Types>>...>::value,
struct __is_reference_wrapper<reference_wrapper<_Up>> : true_type "make_array must be used with an explicit target type when"
{}; "any of the arguments is a reference_wrapper");
};
template <typename _Dest = void, typename... _Types> template <typename _Dest = void, typename... _Types>
constexpr auto constexpr
array<typename __make_array_elem<_Dest, _Types...>::type, sizeof...(_Types)>
make_array(_Types&&... __t) make_array(_Types&&... __t)
-> array<conditional_t<is_void_v<_Dest>,
common_type_t<_Types...>,
_Dest>,
sizeof...(_Types)>
{ {
static_assert(__or_< return {{ std::forward<_Types>(__t)... }};
__not_<is_void<_Dest>>,
__and_<__not_<__is_reference_wrapper<decay_t<_Types>>>...>>
::value,
"make_array cannot be used without an explicit target type "
"if any of the types given is a reference_wrapper");
return {{forward<_Types>(__t)...}};
} }
template <typename _Tp, size_t _Nm, size_t... _Idx> template <typename _Tp, size_t _Nm, size_t... _Idx>
constexpr array<remove_cv_t<_Tp>, _Nm> constexpr array<remove_cv_t<_Tp>, _Nm>
__to_array(_Tp (&__a)[_Nm], __to_array(_Tp (&__a)[_Nm], index_sequence<_Idx...>)
index_sequence<_Idx...>)
{ {
return {{__a[_Idx]...}}; return {{__a[_Idx]...}};
} }
...@@ -93,6 +98,7 @@ template <typename _Tp, size_t _Nm, size_t... _Idx> ...@@ -93,6 +98,7 @@ template <typename _Tp, size_t _Nm, size_t... _Idx>
template <typename _Tp, size_t _Nm> template <typename _Tp, size_t _Nm>
constexpr array<remove_cv_t<_Tp>, _Nm> constexpr array<remove_cv_t<_Tp>, _Nm>
to_array(_Tp (&__a)[_Nm]) to_array(_Tp (&__a)[_Nm])
noexcept(is_nothrow_constructible<remove_cv_t<_Tp>, _Tp&>::value)
{ {
return __to_array(__a, make_index_sequence<_Nm>{}); return __to_array(__a, make_index_sequence<_Nm>{});
} }
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
#include <experimental/array> #include <experimental/array>
#include <functional>
struct MoveOnly struct MoveOnly
{ {
...@@ -27,7 +26,7 @@ struct MoveOnly ...@@ -27,7 +26,7 @@ struct MoveOnly
MoveOnly& operator=(MoveOnly&&) = default; MoveOnly& operator=(MoveOnly&&) = default;
}; };
int main() void test01()
{ {
char x[42]; char x[42];
std::array<char, 42> y = std::experimental::to_array(x); std::array<char, 42> y = std::experimental::to_array(x);
...@@ -45,3 +44,13 @@ int main() ...@@ -45,3 +44,13 @@ int main()
= std::experimental::make_array(1,2L, 3); = std::experimental::make_array(1,2L, 3);
constexpr std::array<MoveOnly, 1> zzz2 = std::experimental::make_array(MoveOnly{}); constexpr std::array<MoveOnly, 1> zzz2 = std::experimental::make_array(MoveOnly{});
} }
void test02()
{
// PR libstdc++/79195
struct A {};
struct B : A {};
struct C : A {};
auto arr = std::experimental::make_array<A>(B{}, C{});
static_assert(std::is_same<decltype(arr), std::array<A, 2>>::value, "");
}
...@@ -24,5 +24,5 @@ int main() ...@@ -24,5 +24,5 @@ int main()
{ {
int dummy; int dummy;
auto bad = std::experimental::make_array(std::ref(dummy)); auto bad = std::experimental::make_array(std::ref(dummy));
// { dg-error "make_array cannot be used without an explicit target type if any of the types given is a reference_wrapper" "" { target *-*-* } 76 } // { dg-error "explicit target type" "" { target *-*-* } 78 }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment