LWG 3074 make scalar types non-deduced in valarray non-member functions
authorJonathan Wakely <jwakely@redhat.com>
Thu, 14 Jun 2018 19:36:54 +0000 (20:36 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 14 Jun 2018 19:36:54 +0000 (20:36 +0100)
* include/bits/valarray_after.h (_DEFINE_EXPR_BINARY_FUNCTION): Change
scalar parameters to be a non-deduced context.
* include/std/valarray (_DEFINE_BINARY_OPERATOR): Likewise. Adjust
whitespace.
* testsuite/26_numerics/valarray/operators.cc: Test scalar operands.
* testsuite/26_numerics/valarray/transcend.cc: New.

From-SVN: r261610

libstdc++-v3/ChangeLog
libstdc++-v3/doc/xml/manual/intro.xml
libstdc++-v3/include/bits/valarray_after.h
libstdc++-v3/include/std/valarray
libstdc++-v3/testsuite/26_numerics/valarray/operators.cc
libstdc++-v3/testsuite/26_numerics/valarray/transcend.cc [new file with mode: 0644]

index 2bfda083ee3eb517a04170ba00e3047b5de7f328..6db36225c1feae13253fcf63656238323d8b1015 100644 (file)
@@ -1,5 +1,13 @@
 2018-06-14  Jonathan Wakely  <jwakely@redhat.com>
 
+       LWG 3074 make scalar types non-deduced in valarray non-member functions
+       * include/bits/valarray_after.h (_DEFINE_EXPR_BINARY_FUNCTION): Change
+       scalar parameters to be a non-deduced context.
+       * include/std/valarray (_DEFINE_BINARY_OPERATOR): Likewise. Adjust
+       whitespace.
+       * testsuite/26_numerics/valarray/operators.cc: Test scalar operands.
+       * testsuite/26_numerics/valarray/transcend.cc: New.
+
        * include/std/tuple (__cpp_lib_tuple_element_t, tuple_element_t):
        Move back to <utility>.
        * include/std/utility (__cpp_lib_tuple_element_t. tuple_element_t):
index 38b6eb36477ba126d5d01f2cbcfa0504a01c59c9..b0fc131d1eb9069ca9b88aecebb9a0a9fe29178f 100644 (file)
@@ -1160,6 +1160,14 @@ requirements of the license of GCC.
     <listitem><para>Add noexcept.
     </para></listitem></varlistentry>
 
+    <varlistentry xml:id="manual.bugs.dr3074"><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="&DR;#3074">3074</link>:
+       <emphasis>Non-member functions for <code>valarray</code> should only deduce from the <code>valarray</code>
+       </emphasis>
+    </term>
+    <listitem><para>Change scalar operands to be non-deduced context, so that
+      they will allow conversions from other types to the value_type.
+    </para></listitem></varlistentry>
+
   </variablelist>
 
  </section>
index bb1a3c9b513547953fa029d4eaa1219080f4cb0a..d2ba76d54c579ad0b5a38d9cd8ff3231eb091c51 100644 (file)
@@ -529,7 +529,8 @@ namespace __detail
                                                                        \
   template<typename _Tp>                                               \
     inline _Expr<_BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
-    _Fun(const valarray<_Tp>& __v, const _Tp& __t)                     \
+    _Fun(const valarray<_Tp>& __v,                                    \
+        const typename valarray<_Tp>::value_type& __t)                \
     {                                                                  \
       typedef _BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp> _Closure;\
       return _Expr<_Closure, _Tp>(_Closure(__v, __t));                 \
@@ -537,7 +538,8 @@ namespace __detail
                                                                       \
   template<typename _Tp>                                               \
     inline _Expr<_BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
-    _Fun(const _Tp& __t, const valarray<_Tp>& __v)                     \
+    _Fun(const typename valarray<_Tp>::value_type& __t,                       \
+        const valarray<_Tp>& __v)                                     \
     {                                                                  \
       typedef _BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp> _Closure;\
       return _Expr<_Closure, _Tp>(_Closure(__t, __v));                 \
index 03e0badfc53d299aff44ce01146b1532d5968c03..7b446fdf9eb92090892a1f1a3d9028f4985eefdf 100644 (file)
@@ -1078,11 +1078,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name)                     \
   template<typename _Tp>                                               \
-    inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt       \
+    inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt       \
     valarray<_Tp>::operator _Op() const                                        \
     {                                                                  \
-      typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                 \
-      typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
+      typedef _UnClos<_Name, _ValArray, _Tp> _Closure;                 \
+      typedef typename __fun<_Name, _Tp>::result_type _Rt;             \
       return _Expr<_Closure, _Rt>(_Closure(*this));                    \
     }
 
@@ -1150,34 +1150,36 @@ _DEFINE_VALARRAY_EXPR_AUGMENTED_ASSIGNMENT(>>, __shift_right)
 
 #define _DEFINE_BINARY_OPERATOR(_Op, _Name)                            \
   template<typename _Tp>                                               \
-    inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,       \
-                 typename __fun<_Name, _Tp>::result_type>               \
+    inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>,      \
+                typename __fun<_Name, _Tp>::result_type>               \
     operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w)   \
     {                                                                  \
-      __glibcxx_assert(__v.size() == __w.size());                       \
-      typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
-      typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
-      return _Expr<_Closure, _Rt>(_Closure(__v, __w));                  \
+      __glibcxx_assert(__v.size() == __w.size());                      \
+      typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure;        \
+      typedef typename __fun<_Name, _Tp>::result_type _Rt;             \
+      return _Expr<_Closure, _Rt>(_Closure(__v, __w));                 \
     }                                                                  \
                                                                        \
   template<typename _Tp>                                               \
-    inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,        \
-                 typename __fun<_Name, _Tp>::result_type>               \
-    operator _Op(const valarray<_Tp>& __v, const _Tp& __t)             \
+    inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>,       \
+                typename __fun<_Name, _Tp>::result_type>               \
+    operator _Op(const valarray<_Tp>& __v,                             \
+                const typename valarray<_Tp>::value_type& __t)         \
     {                                                                  \
       typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure;        \
-      typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
-      return _Expr<_Closure, _Rt>(_Closure(__v, __t));                 \
+      typedef typename __fun<_Name, _Tp>::result_type _Rt;             \
+      return _Expr<_Closure, _Rt>(_Closure(__v, __t));                 \
     }                                                                  \
                                                                        \
   template<typename _Tp>                                               \
-    inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,       \
-                 typename __fun<_Name, _Tp>::result_type>               \
-    operator _Op(const _Tp& __t, const valarray<_Tp>& __v)             \
+    inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>,      \
+                typename __fun<_Name, _Tp>::result_type>               \
+    operator _Op(const typename valarray<_Tp>::value_type& __t,                \
+                const valarray<_Tp>& __v)                              \
     {                                                                  \
-      typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
-      typedef typename __fun<_Name, _Tp>::result_type _Rt;              \
-      return _Expr<_Closure, _Rt>(_Closure(__t, __v));                 \
+      typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure;        \
+      typedef typename __fun<_Name, _Tp>::result_type _Rt;             \
+      return _Expr<_Closure, _Rt>(_Closure(__t, __v));                 \
     }
 
 _DEFINE_BINARY_OPERATOR(+, __plus)
index f85a2b755d7d9f21e61487b0a492cbfca7f2ba51..f0d5c05c0b7b15c2b48058789c5c11fe3a5a9a9d 100644 (file)
@@ -58,9 +58,54 @@ void test02() // check binary operators
   VERIFY( (u>=v)[0] == (1>=3) );
 }
 
+void test03() // check binary operators with scalar operands
+{
+  std::valarray<int> u(1);
+  u[0]=1;
+  long v = 3; // LWG 3074 allows scalar operand to be different to value_type.
+
+  VERIFY( (u+ v)[0] == (1+ 3) );
+  VERIFY( (u- v)[0] == (1- 3) );
+  VERIFY( (u* v)[0] == (1* 3) );
+  VERIFY( (u/ v)[0] == (1/ 3) );
+  VERIFY( (u% v)[0] == (1% 3) );
+  VERIFY( (u^ v)[0] == (1^ 3) );
+  VERIFY( (u& v)[0] == (1& 3) );
+  VERIFY( (u| v)[0] == (1| 3) );
+  VERIFY( (u<<v)[0] == (1<<3) );
+  VERIFY( (u>>v)[0] == (1>>3) );
+  VERIFY( (u&&v)[0] == (1&&3) );
+  VERIFY( (u||v)[0] == (1||3) );
+  VERIFY( (u==v)[0] == (1==3) );
+  VERIFY( (u!=v)[0] == (1!=3) );
+  VERIFY( (u< v)[0] == (1< 3) );
+  VERIFY( (u> v)[0] == (1> 3) );
+  VERIFY( (u<=v)[0] == (1<=3) );
+  VERIFY( (u>=v)[0] == (1>=3) );
+
+  VERIFY( (v+ u)[0] == (3+ 1) );
+  VERIFY( (v- u)[0] == (3- 1) );
+  VERIFY( (v* u)[0] == (3* 1) );
+  VERIFY( (v/ u)[0] == (3/ 1) );
+  VERIFY( (v% u)[0] == (3% 1) );
+  VERIFY( (v^ u)[0] == (3^ 1) );
+  VERIFY( (v& u)[0] == (3& 1) );
+  VERIFY( (v| u)[0] == (3| 1) );
+  VERIFY( (v<<u)[0] == (3<<1) );
+  VERIFY( (v>>u)[0] == (3>>1) );
+  VERIFY( (v&&u)[0] == (3&&1) );
+  VERIFY( (v||u)[0] == (3||1) );
+  VERIFY( (v==u)[0] == (3==1) );
+  VERIFY( (v!=u)[0] == (3!=1) );
+  VERIFY( (v< u)[0] == (3< 1) );
+  VERIFY( (v> u)[0] == (3> 1) );
+  VERIFY( (v<=u)[0] == (3<=1) );
+  VERIFY( (v>=u)[0] == (3>=1) );
+}
+
 int main()
 {
   test01();
   test02();
-  return 0;
+  test03();
 }
diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/transcend.cc b/libstdc++-v3/testsuite/26_numerics/valarray/transcend.cc
new file mode 100644 (file)
index 0000000..e18c30a
--- /dev/null
@@ -0,0 +1,100 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run }
+
+#include <valarray>
+#include <testsuite_hooks.h>
+
+bool eq(double d, double e)
+{
+  return (int)(d * 100) == (int)(e * 100);
+}
+
+void
+test01()
+{
+  std::valarray<double> v(2);
+  v[0] = -0.5;
+  v[1] = 0.25;
+  std::valarray<double> v_abs = abs(v);
+  VERIFY( v_abs[0] == 0.5 );
+  VERIFY( v_abs[1] == 0.25 );
+
+  std::valarray<double> v_acos = acos(v);
+  VERIFY( eq( v_acos[0], 2.09 ) );
+  VERIFY( eq( v_acos[1], 1.31 ) );
+
+  std::valarray<double> v_asin = asin(v);
+  VERIFY( eq( v_asin[0], -0.52 ) );
+  VERIFY( eq( v_asin[1], 0.25 ) );
+
+  std::valarray<double> v_atan = atan(v);
+  VERIFY( eq( v_atan[0], -0.46 ) );
+  VERIFY( eq( v_atan[1], 0.24 ) );
+
+  std::valarray<double> v2(2);
+  v2[0] = 4;
+  v2[1] = 3;
+  std::valarray<double> v_atan2 = atan2(v, v2);
+  VERIFY( eq( v_atan2[0], -0.12 ) );
+  VERIFY( eq( v_atan2[1], 0.08 ) );
+
+  v_atan2 = atan2(v, 4);  // LWG 3074 allows mixed types
+  VERIFY( eq( v_atan2[0], -0.12 ) );
+  VERIFY( eq( v_atan2[1], 0.06 ) );
+
+  v_atan2 = atan2(4, v);  // LWG 3074 allows mixed types
+  VERIFY( eq( v_atan2[0], 1.69 ) );
+  VERIFY( eq( v_atan2[1], 1.50 ) );
+
+  std::valarray<double> v_cos = cos(v);
+  VERIFY( eq( v_cos[0], 0.87 ) );
+  VERIFY( eq( v_cos[1], 0.96 ) );
+
+  std::valarray<double> v_cosh = cosh(v);
+  VERIFY( eq( v_cosh[0], 1.12 ) );
+  VERIFY( eq( v_cosh[1], 1.03 ) );
+
+  std::valarray<double> v_exp = exp(v);
+  VERIFY( eq( v_exp[0], 0.60 ) );
+  VERIFY( eq( v_exp[1], 1.28 ) );
+
+  std::valarray<double> v_log = log(v);
+  VERIFY( eq( v_log[1], -1.38 ) );
+
+  std::valarray<double> v_log10 = log10(v);
+  VERIFY( eq( v_log10[1], -0.60 ) );
+
+  std::valarray<double> v_pow = pow(v, v2);
+  VERIFY( eq( v_pow[0], 0.06 ) );
+  VERIFY( eq( v_pow[1], 0.01 ) );
+
+  v_pow = pow(v, 3);  // LWG 3074 allows mixed types
+  VERIFY( eq( v_pow[0], -0.12 ) );
+  VERIFY( eq( v_pow[1], 0.01 ) );
+
+  v_pow = pow(4, v);  // LWG 3074 allows mixed types
+  VERIFY( eq( v_pow[0], 0.5 ) );
+  VERIFY( eq( v_pow[1], 1.41 ) );
+}
+
+int
+main()
+{
+  test01();
+}