diff mbox series

LWG 3074 make scalar types non-deduced in valarray non-member functions

Message ID 20180614193645.GA28419@redhat.com
State New
Headers show
Series LWG 3074 make scalar types non-deduced in valarray non-member functions | expand

Commit Message

Jonathan Wakely June 14, 2018, 7:36 p.m. UTC
The committee approved this change last week, so that 
operations combining scalars and valarrays work as long as the scalar
is convertible to the valarray's value_type, instead of failing
argument deduction when the types differ.

While testing this I discovered we didn't have any tests for these
operations, so I added some.

	* 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.

Tested powerpc64le-linux, committed to trunk.
commit efbeb8583993d882554f04304cc254db5a3857db
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Jun 14 16:46:05 2018 +0100

    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.
diff mbox series

Patch

diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml
index 38b6eb36477..b0fc131d1eb 100644
--- a/libstdc++-v3/doc/xml/manual/intro.xml
+++ b/libstdc++-v3/doc/xml/manual/intro.xml
@@ -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>
diff --git a/libstdc++-v3/include/bits/valarray_after.h b/libstdc++-v3/include/bits/valarray_after.h
index bb1a3c9b513..d2ba76d54c5 100644
--- a/libstdc++-v3/include/bits/valarray_after.h
+++ b/libstdc++-v3/include/bits/valarray_after.h
@@ -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));                 \
diff --git a/libstdc++-v3/include/std/valarray b/libstdc++-v3/include/std/valarray
index 03e0badfc53..7b446fdf9eb 100644
--- a/libstdc++-v3/include/std/valarray
+++ b/libstdc++-v3/include/std/valarray
@@ -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)
diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/operators.cc b/libstdc++-v3/testsuite/26_numerics/valarray/operators.cc
index f85a2b755d7..f0d5c05c0b7 100644
--- a/libstdc++-v3/testsuite/26_numerics/valarray/operators.cc
+++ b/libstdc++-v3/testsuite/26_numerics/valarray/operators.cc
@@ -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
index 00000000000..e18c30ac35a
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/valarray/transcend.cc
@@ -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();
+}