This adds indirection through a class template partial specialization
so we don't instantiate common_type<_Types...>::type unless we need
it.

        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.

Tested powerpc64le-linux, committed to trunk (because it's only an
experimental TS feature).

commit 16438869aa70f6cad56c094ed26ac73dc75c3847
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Mon Jan 23 15:15:07 2017 +0000

    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.

diff --git a/libstdc++-v3/include/experimental/array 
b/libstdc++-v3/include/experimental/array
index 5899486..11cb924 100644
--- a/libstdc++-v3/include/experimental/array
+++ b/libstdc++-v3/include/experimental/array
@@ -57,35 +57,40 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    * @{
    */
 
-template <typename _Up>
-  struct __is_reference_wrapper : false_type
-  {};
+template<typename _Dest, typename... _Types>
+  struct __make_array_elem
+  {
+    using type = _Dest;
+  };
 
-template <typename _Up>
-  struct __is_reference_wrapper<reference_wrapper<_Up>> : true_type
-  {};
+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
+      {};
+
+    static_assert(!__or_<__is_reference_wrapper<decay_t<_Types>>...>::value,
+                  "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>
-  constexpr auto
+  constexpr
+  array<typename __make_array_elem<_Dest, _Types...>::type, sizeof...(_Types)>
   make_array(_Types&&... __t)
-    -> array<conditional_t<is_void_v<_Dest>,
-                           common_type_t<_Types...>,
-                           _Dest>,
-             sizeof...(_Types)>
   {
-    static_assert(__or_<
-                  __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)...}};
+    return {{ std::forward<_Types>(__t)... }};
   }
 
 template <typename _Tp, size_t _Nm, size_t... _Idx>
   constexpr array<remove_cv_t<_Tp>, _Nm>
-  __to_array(_Tp (&__a)[_Nm],
-             index_sequence<_Idx...>)
+  __to_array(_Tp (&__a)[_Nm], index_sequence<_Idx...>)
   {
     return {{__a[_Idx]...}};
   }
@@ -93,6 +98,7 @@ template <typename _Tp, size_t _Nm, size_t... _Idx>
 template <typename _Tp, size_t _Nm>
   constexpr array<remove_cv_t<_Tp>, _Nm>
   to_array(_Tp (&__a)[_Nm])
+  noexcept(is_nothrow_constructible<remove_cv_t<_Tp>, _Tp&>::value)
   {
     return __to_array(__a, make_index_sequence<_Nm>{});
   }
diff --git a/libstdc++-v3/testsuite/experimental/array/make_array.cc 
b/libstdc++-v3/testsuite/experimental/array/make_array.cc
index 45b1920..1b7d60e 100644
--- a/libstdc++-v3/testsuite/experimental/array/make_array.cc
+++ b/libstdc++-v3/testsuite/experimental/array/make_array.cc
@@ -18,7 +18,6 @@
 // <http://www.gnu.org/licenses/>.
 
 #include <experimental/array>
-#include <functional>
 
 struct MoveOnly
 {
@@ -27,7 +26,7 @@ struct MoveOnly
   MoveOnly& operator=(MoveOnly&&) = default;
 };
 
-int main()
+void test01()
 {
   char x[42];
   std::array<char, 42> y = std::experimental::to_array(x);
@@ -45,3 +44,13 @@ int main()
     = std::experimental::make_array(1,2L, 3);
   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, "");
+}
diff --git a/libstdc++-v3/testsuite/experimental/array/neg.cc 
b/libstdc++-v3/testsuite/experimental/array/neg.cc
index 790933d..3963287 100644
--- a/libstdc++-v3/testsuite/experimental/array/neg.cc
+++ b/libstdc++-v3/testsuite/experimental/array/neg.cc
@@ -24,5 +24,5 @@ int main()
 {
   int 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 }
 }

Reply via email to