diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1a4f6ebb5ae..f3f7c4b271a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2013-10-22 Ed Smith-Rowland <3dw4rd@verizon.net> + + Implement N3779 - User-defined Literals for std::complex, + part 2 of UDL for Standard Library Types + * include/std/complex: Add complex literal operators. + * testsuite/26_numerics/complex/literals/types.cc: New. + * testsuite/26_numerics/complex/literals/values.cc: New. + 2013-10-21 Edward Smith-Rowland <3dw4rd@verizon.net> PR libstdc++/58804 diff --git a/libstdc++-v3/include/std/complex b/libstdc++-v3/include/std/complex index 58edb4f54dd..ff04ae6425e 100644 --- a/libstdc++-v3/include/std/complex +++ b/libstdc++-v3/include/std/complex @@ -1924,6 +1924,40 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION conj(_Tp __x) { return __x; } +#if __cplusplus > 201103L + +inline namespace literals { +inline namespace complex_literals { + + constexpr std::complex + operator""if(long double __num) + { return std::complex{0.0F, static_cast(__num)}; } + + constexpr std::complex + operator""if(unsigned long long __num) + { return std::complex{0.0F, static_cast(__num)}; } + + constexpr std::complex + operator""i(long double __num) + { return std::complex{0.0, static_cast(__num)}; } + + constexpr std::complex + operator""i(unsigned long long __num) + { return std::complex{0.0, static_cast(__num)}; } + + constexpr std::complex + operator""il(long double __num) + { return std::complex{0.0L, __num}; } + + constexpr std::complex + operator""il(unsigned long long __num) + { return std::complex{0.0L, static_cast(__num)}; } + +} // inline namespace complex_literals +} // inline namespace literals + +#endif // C++14 + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/testsuite/26_numerics/complex/literals/types.cc b/libstdc++-v3/testsuite/26_numerics/complex/literals/types.cc new file mode 100644 index 00000000000..e5f9b8c49e4 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/complex/literals/types.cc @@ -0,0 +1,46 @@ +// { dg-options "-std=c++1y" } +// { dg-do compile } + +// Copyright (C) 2013 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 +// . + +#include +#include + +void +test02() +{ + using namespace std::literals::complex_literals; + + static_assert(std::is_same>::value, + "1.0if is std::complex"); + + static_assert(std::is_same>::value, + "1if is std::complex"); + + static_assert(std::is_same>::value, + "1.0i is std::complex"); + + static_assert(std::is_same>::value, + "1i is std::complex"); + + static_assert(std::is_same>::value, + "1.0il is std::complex"); + + static_assert(std::is_same>::value, + "1il is std::complex"); +} diff --git a/libstdc++-v3/testsuite/26_numerics/complex/literals/values.cc b/libstdc++-v3/testsuite/26_numerics/complex/literals/values.cc new file mode 100644 index 00000000000..e56727e49c8 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/complex/literals/values.cc @@ -0,0 +1,48 @@ +// { dg-options "-std=gnu++1y" } +// { dg-do run } + +// Copyright (C) 2013 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 +// . + +#include +#include + +void +test02() +{ + using namespace std::literals::complex_literals; + + std::complex j1 = 1.0if; + std::complex k1 = 1if; + std::complex j2 = 2.0i; + std::complex k2 = 2i; + std::complex j4 = 4.0il; + std::complex k4 = 4il; + + VERIFY( j1 == std::complex(0.0F, 1.0F) ); + VERIFY( k1 == std::complex(0.0F, 1.0F) ); + VERIFY( j2 == std::complex(0.0, 2.0) ); + VERIFY( k2 == std::complex(0.0, 2.0) ); + VERIFY( j4 == std::complex(0.0L, 4.0L) ); + VERIFY( k4 == std::complex(0.0L, 4.0L) ); +} + +int +main() +{ + test02(); +}