This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[v3] complex comparison operators
- From: Benjamin Kosnik <bkoz at redhat dot com>
- To: libstdc++ at gcc dot gnu dot org, Gabriel Dos Reis <gdr at integrable-solutions dot net>
- Date: Fri, 5 Nov 2010 21:07:23 -0700
- Subject: [v3] complex comparison operators
This patch is not applied.
As part of the duration constexpr-ifcation, the utility of constexpr
comparison operators became noticeable.
Original use case:
static_assert(_Period::num > 0, "period must be positive");
or
static_assert(system_clock::duration::min() <
system_clock::duration::zero(), "a clock's minimum duration cannot be
less than its epoch");
So, I went looking for other areas in the library that might benefit
from constexpr comparison operators.
Audit rubric:
1) if static constexpr function can return it
2) if some other class can contain it as a static constexpr data
3) has one of operator ==, !=, >, >=, <, <=
4) implementation of comparison operators calls only constexpr functions
This turns up std::complex operators == and != as candidates.
Close:
1. tuple, pair, and <random>'s engines have operator ==, but no
constexpr construction.
2. array, but the the operator implementations are not constexpr.
I think this should go in, but am waiting for a second.
The required complex specializations are
literal types and comparing them at compile time seems worth the
(minimal) effort. A potential problem (?) is that the primary std::complex
template does not mark real(), imag() or any of the constructors as
constexpr. (I believe this is incorrect: it means that compile-time
behavior is different for specializations and primary templates, which
seems misleading.)
Since the primary template for complex is not guaranteed to be useful
for other than the required classes, I'm not quite sure this is a
reason that is credible.
tested x86_64/linux
-benjamin
2010-11-05 Benjamin Kosnik <bkoz@redhat.com>
* include/std/complex: Mark comparison operators constexpr.
* testsuite/26_numerics/complex/comparison_operators/constexpr.cc:
New.
Index: include/std/complex
===================================================================
--- include/std/complex (revision 166391)
+++ include/std/complex (working copy)
@@ -447,17 +447,17 @@
//@{
/// Return true if @a x is equal to @a y.
template<typename _Tp>
- inline bool
+ inline _GLIBCXX_CONSTEXPR bool
operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
{ return __x.real() == __y.real() && __x.imag() == __y.imag(); }
template<typename _Tp>
- inline bool
+ inline _GLIBCXX_CONSTEXPR bool
operator==(const complex<_Tp>& __x, const _Tp& __y)
{ return __x.real() == __y && __x.imag() == _Tp(); }
template<typename _Tp>
- inline bool
+ inline _GLIBCXX_CONSTEXPR bool
operator==(const _Tp& __x, const complex<_Tp>& __y)
{ return __x == __y.real() && _Tp() == __y.imag(); }
//@}
@@ -465,17 +465,17 @@
//@{
/// Return false if @a x is equal to @a y.
template<typename _Tp>
- inline bool
+ inline _GLIBCXX_CONSTEXPR bool
operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
{ return __x.real() != __y.real() || __x.imag() != __y.imag(); }
template<typename _Tp>
- inline bool
+ inline _GLIBCXX_CONSTEXPR bool
operator!=(const complex<_Tp>& __x, const _Tp& __y)
{ return __x.real() != __y || __x.imag() != _Tp(); }
template<typename _Tp>
- inline bool
+ inline _GLIBCXX_CONSTEXPR bool
operator!=(const _Tp& __x, const complex<_Tp>& __y)
{ return __x != __y.real() || _Tp() != __y.imag(); }
//@}
Index: testsuite/26_numerics/complex/comparison_operators/constexpr.cc
===================================================================
--- testsuite/26_numerics/complex/comparison_operators/constexpr.cc (revision 0)
+++ testsuite/26_numerics/complex/comparison_operators/constexpr.cc (revision 0)
@@ -0,0 +1,39 @@
+// { dg-do compile }
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2010 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/>.
+
+#include <complex>
+#include <testsuite_common_types.h>
+
+int main()
+{
+ __gnu_test::constexpr_comparison_eq_ne test;
+ test.operator()<std::complex<float>>();
+ test.operator()<std::complex<float>, float>();
+ test.operator()<float,std::complex<float>>();
+
+ test.operator()<std::complex<double>>();
+ test.operator()<std::complex<double>, double>();
+ test.operator()<double,std::complex<double>>();
+
+ test.operator()<std::complex<long double>>();
+ test.operator()<std::complex<long double>, long double>();
+ test.operator()<long double,std::complex<long double>>();
+ return 0;
+}