Implement constexpr (n3302) and fix operator *= and /=

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@187529 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2013-07-31 21:02:34 +00:00
parent 0777473911
commit a61e6f8705
14 changed files with 329 additions and 104 deletions

View File

@ -23,12 +23,12 @@ class complex
public: public:
typedef T value_type; typedef T value_type;
complex(const T& re = T(), const T& im = T()); complex(const T& re = T(), const T& im = T()); // constexpr in C++14
complex(const complex&); complex(const complex&); // constexpr in C++14
template<class X> complex(const complex<X>&); template<class X> complex(const complex<X>&); // constexpr in C++14
T real() const; T real() const; // constexpr in C++14
T imag() const; T imag() const; // constexpr in C++14
void real(T); void real(T);
void imag(T); void imag(T);
@ -149,12 +149,12 @@ template<class T> complex<T> operator/(const complex<T>&, const T&);
template<class T> complex<T> operator/(const T&, const complex<T>&); template<class T> complex<T> operator/(const T&, const complex<T>&);
template<class T> complex<T> operator+(const complex<T>&); template<class T> complex<T> operator+(const complex<T>&);
template<class T> complex<T> operator-(const complex<T>&); template<class T> complex<T> operator-(const complex<T>&);
template<class T> bool operator==(const complex<T>&, const complex<T>&); template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
template<class T> bool operator==(const complex<T>&, const T&); template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
template<class T> bool operator==(const T&, const complex<T>&); template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
template<class T> bool operator!=(const complex<T>&, const complex<T>&); template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
template<class T> bool operator!=(const complex<T>&, const T&); template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
template<class T> bool operator!=(const T&, const complex<T>&); template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
template<class T, class charT, class traits> template<class T, class charT, class traits>
basic_istream<charT, traits>& basic_istream<charT, traits>&
@ -165,17 +165,17 @@ template<class T, class charT, class traits>
// 26.3.7 values: // 26.3.7 values:
template<class T> T real(const complex<T>&); template<class T> T real(const complex<T>&); // constexpr in C++14
long double real(long double); long double real(long double); // constexpr in C++14
double real(double); double real(double); // constexpr in C++14
template<Integral T> double real(T); template<Integral T> double real(T); // constexpr in C++14
float real(float); float real(float); // constexpr in C++14
template<class T> T imag(const complex<T>&); template<class T> T imag(const complex<T>&); // constexpr in C++14
long double imag(long double); long double imag(long double); // constexpr in C++14
double imag(double); double imag(double); // constexpr in C++14
template<Integral T> double imag(T); template<Integral T> double imag(T); // constexpr in C++14
float imag(float); float imag(float); // constexpr in C++14
template<class T> T abs(const complex<T>&); template<class T> T abs(const complex<T>&);
@ -269,15 +269,15 @@ private:
value_type __re_; value_type __re_;
value_type __im_; value_type __im_;
public: public:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
complex(const value_type& __re = value_type(), const value_type& __im = value_type()) complex(const value_type& __re = value_type(), const value_type& __im = value_type())
: __re_(__re), __im_(__im) {} : __re_(__re), __im_(__im) {}
template<class _Xp> _LIBCPP_INLINE_VISIBILITY template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
complex(const complex<_Xp>& __c) complex(const complex<_Xp>& __c)
: __re_(__c.real()), __im_(__c.imag()) {} : __re_(__c.real()), __im_(__c.imag()) {}
_LIBCPP_INLINE_VISIBILITY value_type real() const {return __re_;} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
_LIBCPP_INLINE_VISIBILITY value_type imag() const {return __im_;} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
_LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
_LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
@ -309,12 +309,12 @@ public:
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
{ {
*this = *this * __c; *this = *this * complex(__c.real(), __c.imag());
return *this; return *this;
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
{ {
*this = *this / __c; *this = *this / complex(__c.real(), __c.imag());
return *this; return *this;
} }
}; };
@ -368,12 +368,12 @@ public:
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
{ {
*this = *this * __c; *this = *this * complex(__c.real(), __c.imag());
return *this; return *this;
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
{ {
*this = *this / __c; *this = *this / complex(__c.real(), __c.imag());
return *this; return *this;
} }
}; };
@ -424,12 +424,12 @@ public:
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
{ {
*this = *this * __c; *this = *this * complex(__c.real(), __c.imag());
return *this; return *this;
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
{ {
*this = *this / __c; *this = *this / complex(__c.real(), __c.imag());
return *this; return *this;
} }
}; };
@ -480,12 +480,12 @@ public:
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
{ {
*this = *this * __c; *this = *this * complex(__c.real(), __c.imag());
return *this; return *this;
} }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
{ {
*this = *this / __c; *this = *this / complex(__c.real(), __c.imag());
return *this; return *this;
} }
}; };
@ -740,7 +740,7 @@ operator-(const complex<_Tp>& __x)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool bool
operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
{ {
@ -748,7 +748,7 @@ operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool bool
operator==(const complex<_Tp>& __x, const _Tp& __y) operator==(const complex<_Tp>& __x, const _Tp& __y)
{ {
@ -756,7 +756,7 @@ operator==(const complex<_Tp>& __x, const _Tp& __y)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool bool
operator==(const _Tp& __x, const complex<_Tp>& __y) operator==(const _Tp& __x, const complex<_Tp>& __y)
{ {
@ -764,7 +764,7 @@ operator==(const _Tp& __x, const complex<_Tp>& __y)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool bool
operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
{ {
@ -772,7 +772,7 @@ operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool bool
operator!=(const complex<_Tp>& __x, const _Tp& __y) operator!=(const complex<_Tp>& __x, const _Tp& __y)
{ {
@ -780,7 +780,7 @@ operator!=(const complex<_Tp>& __x, const _Tp& __y)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool bool
operator!=(const _Tp& __x, const complex<_Tp>& __y) operator!=(const _Tp& __x, const complex<_Tp>& __y)
{ {
@ -792,21 +792,21 @@ operator!=(const _Tp& __x, const complex<_Tp>& __y)
// real // real
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp _Tp
real(const complex<_Tp>& __c) real(const complex<_Tp>& __c)
{ {
return __c.real(); return __c.real();
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
long double long double
real(long double __re) real(long double __re)
{ {
return __re; return __re;
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
double double
real(double __re) real(double __re)
{ {
@ -814,7 +814,7 @@ real(double __re)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename enable_if typename enable_if
< <
is_integral<_Tp>::value, is_integral<_Tp>::value,
@ -825,7 +825,7 @@ real(_Tp __re)
return __re; return __re;
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
float float
real(float __re) real(float __re)
{ {
@ -835,21 +835,21 @@ real(float __re)
// imag // imag
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp _Tp
imag(const complex<_Tp>& __c) imag(const complex<_Tp>& __c)
{ {
return __c.imag(); return __c.imag();
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
long double long double
imag(long double __re) imag(long double __re)
{ {
return 0; return 0;
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
double double
imag(double __re) imag(double __re)
{ {
@ -857,7 +857,7 @@ imag(double __re)
} }
template<class _Tp> template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
typename enable_if typename enable_if
< <
is_integral<_Tp>::value, is_integral<_Tp>::value,
@ -868,7 +868,7 @@ imag(_Tp __re)
return 0; return 0;
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
float float
imag(float __re) imag(float __re)
{ {

View File

@ -19,29 +19,41 @@
#include "../cases.h" #include "../cases.h"
template <class T> template <class T, int x>
void void
test(T x, typename std::enable_if<std::is_integral<T>::value>::type* = 0) test(typename std::enable_if<std::is_integral<T>::value>::type* = 0)
{ {
static_assert((std::is_same<decltype(std::imag(x)), double>::value), ""); static_assert((std::is_same<decltype(std::imag(T(x))), double>::value), "");
assert(std::imag(x) == 0); assert(std::imag(x) == 0);
#if _LIBCPP_STD_VER > 11
constexpr T val {x};
static_assert(std::imag(val) == 0, "");
constexpr std::complex<T> t{val, val};
static_assert(t.imag() == x, "" );
#endif
} }
template <class T> template <class T, int x>
void void
test(T x, typename std::enable_if<!std::is_integral<T>::value>::type* = 0) test(typename std::enable_if<!std::is_integral<T>::value>::type* = 0)
{ {
static_assert((std::is_same<decltype(std::imag(x)), T>::value), ""); static_assert((std::is_same<decltype(std::imag(T(x))), T>::value), "");
assert(std::imag(x) == 0); assert(std::imag(x) == 0);
#if _LIBCPP_STD_VER > 11
constexpr T val {x};
static_assert(std::imag(val) == 0, "");
constexpr std::complex<T> t{val, val};
static_assert(t.imag() == x, "" );
#endif
} }
template <class T> template <class T>
void void
test() test()
{ {
test<T>(0); test<T, 0>();
test<T>(1); test<T, 1>();
test<T>(10); test<T, 10>();
} }
int main() int main()

View File

@ -19,29 +19,41 @@
#include "../cases.h" #include "../cases.h"
template <class T> template <class T, int x>
void void
test(T x, typename std::enable_if<std::is_integral<T>::value>::type* = 0) test(typename std::enable_if<std::is_integral<T>::value>::type* = 0)
{ {
static_assert((std::is_same<decltype(std::real(x)), double>::value), ""); static_assert((std::is_same<decltype(std::real(T(x))), double>::value), "");
assert(std::real(x) == x); assert(std::real(x) == x);
#if _LIBCPP_STD_VER > 11
constexpr T val {x};
static_assert(std::real(val) == val, "");
constexpr std::complex<T> t{val, val};
static_assert(t.real() == x, "" );
#endif
} }
template <class T> template <class T, int x>
void void
test(T x, typename std::enable_if<!std::is_integral<T>::value>::type* = 0) test(typename std::enable_if<!std::is_integral<T>::value>::type* = 0)
{ {
static_assert((std::is_same<decltype(std::real(x)), T>::value), ""); static_assert((std::is_same<decltype(std::real(T(x))), T>::value), "");
assert(std::real(x) == x); assert(std::real(x) == x);
#if _LIBCPP_STD_VER > 11
constexpr T val {x};
static_assert(std::real(val) == val, "");
constexpr std::complex<T> t{val, val};
static_assert(t.real() == x, "" );
#endif
} }
template <class T> template <class T>
void void
test() test()
{ {
test<T>(0); test<T, 0>();
test<T>(1); test<T, 1>();
test<T>(10); test<T, 10>();
} }
int main() int main()

View File

@ -28,6 +28,21 @@ test()
c /= c2; c /= c2;
assert(c.real() == 1); assert(c.real() == 1);
assert(c.imag() == 0); assert(c.imag() == 0);
std::complex<T> c3;
c3 = c;
std::complex<int> ic (1,1);
c3 /= ic;
assert(c3.real() == 0.5);
assert(c3.imag() == -0.5);
c3 = c;
std::complex<float> fc (1,1);
c3 /= fc;
assert(c3.real() == 0.5);
assert(c3.imag() == -0.5);
} }
int main() int main()

View File

@ -28,6 +28,20 @@ test()
c -= c2; c -= c2;
assert(c.real() == -3); assert(c.real() == -3);
assert(c.imag() == -5); assert(c.imag() == -5);
std::complex<T> c3;
c3 = c;
std::complex<int> ic (1,1);
c3 -= ic;
assert(c3.real() == -4);
assert(c3.imag() == -6);
c3 = c;
std::complex<float> fc (1,1);
c3 -= fc;
assert(c3.real() == -4);
assert(c3.imag() == -6);
} }
int main() int main()

View File

@ -28,6 +28,20 @@ test()
c += c2; c += c2;
assert(c.real() == 3); assert(c.real() == 3);
assert(c.imag() == 5); assert(c.imag() == 5);
std::complex<T> c3;
c3 = c;
std::complex<int> ic (1,1);
c3 += ic;
assert(c3.real() == 4);
assert(c3.imag() == 6);
c3 = c;
std::complex<float> fc (1,1);
c3 += fc;
assert(c3.real() == 4);
assert(c3.imag() == 6);
} }
int main() int main()

View File

@ -28,6 +28,20 @@ test()
c *= c2; c *= c2;
assert(c.real() == -4); assert(c.real() == -4);
assert(c.imag() == 7.5); assert(c.imag() == 7.5);
std::complex<T> c3;
c3 = c;
std::complex<int> ic (1,1);
c3 *= ic;
assert(c3.real() == -11.5);
assert(c3.imag() == 3.5);
c3 = c;
std::complex<float> fc (1,1);
c3 *= fc;
assert(c3.real() == -11.5);
assert(c3.imag() == 3.5);
} }
int main() int main()

View File

@ -15,6 +15,23 @@
#include <complex> #include <complex>
#include <cassert> #include <cassert>
template <class T>
void
test_constexpr()
{
#if _LIBCPP_STD_VER > 11
constexpr std::complex<T> c1;
static_assert(c1.real() == 0, "");
static_assert(c1.imag() == 0, "");
constexpr std::complex<T> c2(3);
static_assert(c2.real() == 3, "");
static_assert(c2.imag() == 0, "");
constexpr std::complex<T> c3(3, 4);
static_assert(c3.real() == 3, "");
static_assert(c3.imag() == 4, "");
#endif
}
template <class T> template <class T>
void void
test() test()
@ -34,6 +51,8 @@ test()
c.imag(-5.5); c.imag(-5.5);
assert(c.real() == -4.5); assert(c.real() == -4.5);
assert(c.imag() == -5.5); assert(c.imag() == -5.5);
test_constexpr<T> ();
} }
int main() int main()
@ -41,4 +60,5 @@ int main()
test<float>(); test<float>();
test<double>(); test<double>();
test<long double>(); test<long double>();
test_constexpr<int> ();
} }

View File

@ -18,9 +18,20 @@
template <class T> template <class T>
void void
test(const std::complex<T>& lhs, const std::complex<T>& rhs, bool x) test_constexpr()
{ {
assert((lhs == rhs) == x); #if _LIBCPP_STD_VER > 11
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr std::complex<T> rhs(1.5, -2.5);
static_assert( !(lhs == rhs), "");
}
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr std::complex<T> rhs(1.5, 2.5);
static_assert(lhs == rhs, "");
}
#endif
} }
template <class T> template <class T>
@ -30,13 +41,14 @@ test()
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
std::complex<T> rhs(1.5, -2.5); std::complex<T> rhs(1.5, -2.5);
test(lhs, rhs, false); assert( !(lhs == rhs));
} }
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
std::complex<T> rhs(1.5, 2.5); std::complex<T> rhs(1.5, 2.5);
test(lhs, rhs, true); assert(lhs == rhs);
} }
test_constexpr<T> ();
} }
int main() int main()
@ -44,4 +56,5 @@ int main()
test<float>(); test<float>();
test<double>(); test<double>();
test<long double>(); test<long double>();
// test_constexpr<int> ();
} }

View File

@ -18,9 +18,30 @@
template <class T> template <class T>
void void
test(const std::complex<T>& lhs, const T& rhs, bool x) test_constexpr()
{ {
assert((lhs == rhs) == x); #if _LIBCPP_STD_VER > 11
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr T rhs(-2.5);
static_assert(!(lhs == rhs), "");
}
{
constexpr std::complex<T> lhs(1.5, 0);
constexpr T rhs(-2.5);
static_assert(!(lhs == rhs), "");
}
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr T rhs(1.5);
static_assert(!(lhs == rhs), "");
}
{
constexpr std::complex<T> lhs(1.5, 0);
constexpr T rhs(1.5);
static_assert( (lhs == rhs), "");
}
#endif
} }
template <class T> template <class T>
@ -30,28 +51,31 @@ test()
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
T rhs(-2.5); T rhs(-2.5);
test(lhs, rhs, false); assert(!(lhs == rhs));
} }
{ {
std::complex<T> lhs(1.5, 0); std::complex<T> lhs(1.5, 0);
T rhs(-2.5); T rhs(-2.5);
test(lhs, rhs, false); assert(!(lhs == rhs));
} }
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
T rhs(1.5); T rhs(1.5);
test(lhs, rhs, false); assert(!(lhs == rhs));
} }
{ {
std::complex<T> lhs(1.5, 0); std::complex<T> lhs(1.5, 0);
T rhs(1.5); T rhs(1.5);
test(lhs, rhs, true); assert( (lhs == rhs));
}
test_constexpr<T> ();
} }
}
int main() int main()
{ {
test<float>(); test<float>();
test<double>(); test<double>();
test<long double>(); test<long double>();
// test_constexpr<int> ();
} }

View File

@ -18,11 +18,23 @@
template <class T> template <class T>
void void
test(const std::complex<T>& lhs, const std::complex<T>& rhs, bool x) test_constexpr()
{ {
assert((lhs != rhs) == x); #if _LIBCPP_STD_VER > 11
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr std::complex<T> rhs(1.5, -2.5);
static_assert(lhs != rhs, "");
}
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr std::complex<T> rhs(1.5, 2.5);
static_assert(!(lhs != rhs), "" );
}
#endif
} }
template <class T> template <class T>
void void
test() test()
@ -30,18 +42,21 @@ test()
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
std::complex<T> rhs(1.5, -2.5); std::complex<T> rhs(1.5, -2.5);
test(lhs, rhs, true); assert(lhs != rhs);
} }
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
std::complex<T> rhs(1.5, 2.5); std::complex<T> rhs(1.5, 2.5);
test(lhs, rhs, false); assert(!(lhs != rhs));
}
test_constexpr<T> ();
} }
}
int main() int main()
{ {
test<float>(); test<float>();
test<double>(); test<double>();
test<long double>(); test<long double>();
// test_constexpr<int> ();
} }

View File

@ -18,9 +18,30 @@
template <class T> template <class T>
void void
test(const std::complex<T>& lhs, const T& rhs, bool x) test_constexpr()
{ {
assert((lhs != rhs) == x); #if _LIBCPP_STD_VER > 11
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr T rhs(-2.5);
static_assert(lhs != rhs, "");
}
{
constexpr std::complex<T> lhs(1.5, 0);
constexpr T rhs(-2.5);
static_assert(lhs != rhs, "");
}
{
constexpr std::complex<T> lhs(1.5, 2.5);
constexpr T rhs(1.5);
static_assert(lhs != rhs, "");
}
{
constexpr std::complex<T> lhs(1.5, 0);
constexpr T rhs(1.5);
static_assert( !(lhs != rhs), "");
}
#endif
} }
template <class T> template <class T>
@ -30,23 +51,25 @@ test()
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
T rhs(-2.5); T rhs(-2.5);
test(lhs, rhs, true); assert(lhs != rhs);
} }
{ {
std::complex<T> lhs(1.5, 0); std::complex<T> lhs(1.5, 0);
T rhs(-2.5); T rhs(-2.5);
test(lhs, rhs, true); assert(lhs != rhs);
} }
{ {
std::complex<T> lhs(1.5, 2.5); std::complex<T> lhs(1.5, 2.5);
T rhs(1.5); T rhs(1.5);
test(lhs, rhs, true); assert(lhs != rhs);
} }
{ {
std::complex<T> lhs(1.5, 0); std::complex<T> lhs(1.5, 0);
T rhs(1.5); T rhs(1.5);
test(lhs, rhs, false); assert( !(lhs != rhs));
} }
test_constexpr<T> ();
} }
int main() int main()
@ -54,4 +77,5 @@ int main()
test<float>(); test<float>();
test<double>(); test<double>();
test<long double>(); test<long double>();
} // test_constexpr<int> ();
}

View File

@ -18,9 +18,30 @@
template <class T> template <class T>
void void
test(const T& lhs, const std::complex<T>& rhs, bool x) test_constexpr()
{ {
assert((lhs == rhs) == x); #if _LIBCPP_STD_VER > 11
{
constexpr T lhs(-2.5);
constexpr std::complex<T> rhs(1.5, 2.5);
static_assert(!(lhs == rhs), "");
}
{
constexpr T lhs(-2.5);
constexpr std::complex<T> rhs(1.5, 0);
static_assert(!(lhs == rhs), "");
}
{
constexpr T lhs(1.5);
constexpr std::complex<T> rhs(1.5, 2.5);
static_assert(!(lhs == rhs), "");
}
{
constexpr T lhs(1.5);
constexpr std::complex<T> rhs(1.5, 0);
static_assert(lhs == rhs, "");
}
#endif
} }
template <class T> template <class T>
@ -30,23 +51,25 @@ test()
{ {
T lhs(-2.5); T lhs(-2.5);
std::complex<T> rhs(1.5, 2.5); std::complex<T> rhs(1.5, 2.5);
test(lhs, rhs, false); assert(!(lhs == rhs));
} }
{ {
T lhs(-2.5); T lhs(-2.5);
std::complex<T> rhs(1.5, 0); std::complex<T> rhs(1.5, 0);
test(lhs, rhs, false); assert(!(lhs == rhs));
} }
{ {
T lhs(1.5); T lhs(1.5);
std::complex<T> rhs(1.5, 2.5); std::complex<T> rhs(1.5, 2.5);
test(lhs, rhs, false); assert(!(lhs == rhs));
} }
{ {
T lhs(1.5); T lhs(1.5);
std::complex<T> rhs(1.5, 0); std::complex<T> rhs(1.5, 0);
test(lhs, rhs, true); assert(lhs == rhs);
} }
test_constexpr<T> ();
} }
int main() int main()
@ -54,4 +77,5 @@ int main()
test<float>(); test<float>();
test<double>(); test<double>();
test<long double>(); test<long double>();
// test_constexpr<int>();
} }

View File

@ -18,9 +18,30 @@
template <class T> template <class T>
void void
test(const T& lhs, const std::complex<T>& rhs, bool x) test_constexpr()
{ {
assert((lhs != rhs) == x); #if _LIBCPP_STD_VER > 11
{
constexpr T lhs(-2.5);
constexpr std::complex<T> rhs(1.5, 2.5);
static_assert (lhs != rhs, "");
}
{
constexpr T lhs(-2.5);
constexpr std::complex<T> rhs(1.5, 0);
static_assert (lhs != rhs, "");
}
{
constexpr T lhs(1.5);
constexpr std::complex<T> rhs(1.5, 2.5);
static_assert (lhs != rhs, "");
}
{
constexpr T lhs(1.5);
constexpr std::complex<T> rhs(1.5, 0);
static_assert (!(lhs != rhs), "");
}
#endif
} }
template <class T> template <class T>
@ -30,28 +51,31 @@ test()
{ {
T lhs(-2.5); T lhs(-2.5);
std::complex<T> rhs(1.5, 2.5); std::complex<T> rhs(1.5, 2.5);
test(lhs, rhs, true); assert (lhs != rhs);
} }
{ {
T lhs(-2.5); T lhs(-2.5);
std::complex<T> rhs(1.5, 0); std::complex<T> rhs(1.5, 0);
test(lhs, rhs, true); assert (lhs != rhs);
} }
{ {
T lhs(1.5); T lhs(1.5);
std::complex<T> rhs(1.5, 2.5); std::complex<T> rhs(1.5, 2.5);
test(lhs, rhs, true); assert (lhs != rhs);
} }
{ {
T lhs(1.5); T lhs(1.5);
std::complex<T> rhs(1.5, 0); std::complex<T> rhs(1.5, 0);
test(lhs, rhs, false); assert (!(lhs != rhs));
}
test_constexpr<T> ();
} }
}
int main() int main()
{ {
test<float>(); test<float>();
test<double>(); test<double>();
test<long double>(); test<long double>();
// test_constexpr<int>();
} }