From 320c80fecf6b50cbc89840189b90a972baf4d6fc Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sat, 5 Oct 2013 21:19:49 +0000 Subject: [PATCH] Implement literal suffixes for compled git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@192048 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/complex | 41 +++++++++++++++ .../complex.literals/literals.pass.cpp | 51 +++++++++++++++++++ .../complex.literals/literals1.fail.cpp | 20 ++++++++ .../complex.literals/literals1.pass.cpp | 43 ++++++++++++++++ .../complex.literals/literals2.pass.cpp | 43 ++++++++++++++++ 5 files changed, 198 insertions(+) create mode 100644 test/numerics/complex.number/complex.literals/literals.pass.cpp create mode 100644 test/numerics/complex.number/complex.literals/literals1.fail.cpp create mode 100644 test/numerics/complex.number/complex.literals/literals1.pass.cpp create mode 100644 test/numerics/complex.number/complex.literals/literals2.pass.cpp diff --git a/include/complex b/include/complex index e1d72f4d..2943da1d 100644 --- a/include/complex +++ b/include/complex @@ -1521,6 +1521,47 @@ operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) return __os << __s.str(); } +#if _LIBCPP_STD_VER > 11 +// Literal suffix for complex number literals [complex.literals] +inline namespace literals +{ + inline namespace complex_literals + { + constexpr complex operator""il(long double __im) + { + return { 0.0l, __im }; + } + + constexpr complex operator""il(unsigned long long __im) + { + return { 0.0l, static_cast(__im) }; + } + + + constexpr complex operator""i(long double __im) + { + return { 0.0, static_cast(__im) }; + } + + constexpr complex operator""i(unsigned long long __im) + { + return { 0.0, static_cast(__im) }; + } + + + constexpr complex operator""if(long double __im) + { + return { 0.0f, static_cast(__im) }; + } + + constexpr complex operator""if(unsigned long long __im) + { + return { 0.0f, static_cast(__im) }; + } + } +} +#endif + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_COMPLEX diff --git a/test/numerics/complex.number/complex.literals/literals.pass.cpp b/test/numerics/complex.number/complex.literals/literals.pass.cpp new file mode 100644 index 00000000..45b59914 --- /dev/null +++ b/test/numerics/complex.number/complex.literals/literals.pass.cpp @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +#include +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + using namespace std::literals::complex_literals; + +// Make sure the types are right + static_assert ( std::is_same>::value, "" ); + static_assert ( std::is_same>::value, "" ); + static_assert ( std::is_same>::value, "" ); + static_assert ( std::is_same>::value, "" ); + static_assert ( std::is_same>::value, "" ); + static_assert ( std::is_same>::value, "" ); + + { + std::complex c1 = 3.0il; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3il; + assert ( c1 == c2 ); + } + + { + std::complex c1 = 3.0i; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3i; + assert ( c1 == c2 ); + } + + { + std::complex c1 = 3.0if; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3if; + assert ( c1 == c2 ); + } + +#endif +} diff --git a/test/numerics/complex.number/complex.literals/literals1.fail.cpp b/test/numerics/complex.number/complex.literals/literals1.fail.cpp new file mode 100644 index 00000000..0f31d595 --- /dev/null +++ b/test/numerics/complex.number/complex.literals/literals1.fail.cpp @@ -0,0 +1,20 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::complex foo = 1.0if; // should fail w/conversion operator not found +#else +#error +#endif +} diff --git a/test/numerics/complex.number/complex.literals/literals1.pass.cpp b/test/numerics/complex.number/complex.literals/literals1.pass.cpp new file mode 100644 index 00000000..46903bbd --- /dev/null +++ b/test/numerics/complex.number/complex.literals/literals1.pass.cpp @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +#include +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + using namespace std::literals; + + { + std::complex c1 = 3.0il; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3il; + assert ( c1 == c2 ); + } + + { + std::complex c1 = 3.0i; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3i; + assert ( c1 == c2 ); + } + + { + std::complex c1 = 3.0if; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3if; + assert ( c1 == c2 ); + } + +#endif +} diff --git a/test/numerics/complex.number/complex.literals/literals2.pass.cpp b/test/numerics/complex.number/complex.literals/literals2.pass.cpp new file mode 100644 index 00000000..8bd8acd9 --- /dev/null +++ b/test/numerics/complex.number/complex.literals/literals2.pass.cpp @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +#include +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + using namespace std; + + { + std::complex c1 = 3.0il; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3il; + assert ( c1 == c2 ); + } + + { + std::complex c1 = 3.0i; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3i; + assert ( c1 == c2 ); + } + + { + std::complex c1 = 3.0if; + assert ( c1 == std::complex(0, 3.0)); + auto c2 = 3if; + assert ( c1 == c2 ); + } + +#endif +}