|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// <complex> |
| 10 | + |
| 11 | +// template<class T, class U> complex<__promote<T, U>::type> pow(const complex<T>&, const U&); |
| 12 | +// template<class T, class U> complex<__promote<T, U>::type> pow(const complex<T>&, const complex<U>&); |
| 13 | +// template<class T, class U> complex<__promote<T, U>::type> pow(const T&, const complex<U>&); |
| 14 | + |
| 15 | +// Test that these additional overloads are free from catching std::complex<non-floating-point>, |
| 16 | +// which is expected by several 3rd party libraries, see https://github.com/llvm/llvm-project/issues/109858. |
| 17 | +// |
| 18 | +// Note that we reserve the right to break this in the future if we have a reason to, but for the time being, |
| 19 | +// make sure we don't break this property unintentionally. |
| 20 | +#include <cassert> |
| 21 | +#include <cmath> |
| 22 | +#include <complex> |
| 23 | +#include <type_traits> |
| 24 | + |
| 25 | +#include "test_macros.h" |
| 26 | + |
| 27 | +namespace usr { |
| 28 | +struct usr_tag {}; |
| 29 | + |
| 30 | +template <class T, class U> |
| 31 | +typename std::enable_if<(std::is_same<T, usr_tag>::value && std::is_floating_point<U>::value) || |
| 32 | + (std::is_floating_point<T>::value && std::is_same<U, usr_tag>::value), |
| 33 | + int>::type |
| 34 | +pow(const T&, const std::complex<U>&) { |
| 35 | + return std::is_same<T, usr_tag>::value ? 0 : 1; |
| 36 | +} |
| 37 | + |
| 38 | +template <class T, class U> |
| 39 | +typename std::enable_if<(std::is_same<T, usr_tag>::value && std::is_floating_point<U>::value) || |
| 40 | + (std::is_floating_point<T>::value && std::is_same<U, usr_tag>::value), |
| 41 | + int>::type |
| 42 | +pow(const std::complex<T>&, const U&) { |
| 43 | + return std::is_same<U, usr_tag>::value ? 2 : 3; |
| 44 | +} |
| 45 | + |
| 46 | +template <class T, class U> |
| 47 | +typename std::enable_if<(std::is_same<T, usr_tag>::value && std::is_floating_point<U>::value) || |
| 48 | + (std::is_floating_point<T>::value && std::is_same<U, usr_tag>::value), |
| 49 | + int>::type |
| 50 | +pow(const std::complex<T>&, const std::complex<U>&) { |
| 51 | + return std::is_same<T, usr_tag>::value ? 4 : 5; |
| 52 | +} |
| 53 | +} // namespace usr |
| 54 | + |
| 55 | +int main(int, char**) { |
| 56 | + using std::pow; |
| 57 | + using usr::pow; |
| 58 | + |
| 59 | + usr::usr_tag tag; |
| 60 | + const std::complex<usr::usr_tag> ctag; |
| 61 | + |
| 62 | + assert(pow(tag, std::complex<float>(1.0f)) == 0); |
| 63 | + assert(pow(std::complex<float>(1.0f), tag) == 2); |
| 64 | + assert(pow(tag, std::complex<double>(1.0)) == 0); |
| 65 | + assert(pow(std::complex<double>(1.0), tag) == 2); |
| 66 | + assert(pow(tag, std::complex<long double>(1.0l)) == 0); |
| 67 | + assert(pow(std::complex<long double>(1.0l), tag) == 2); |
| 68 | + |
| 69 | + assert(pow(1.0f, ctag) == 1); |
| 70 | + assert(pow(ctag, 1.0f) == 3); |
| 71 | + assert(pow(1.0, ctag) == 1); |
| 72 | + assert(pow(ctag, 1.0) == 3); |
| 73 | + assert(pow(1.0l, ctag) == 1); |
| 74 | + assert(pow(ctag, 1.0l) == 3); |
| 75 | + |
| 76 | + assert(pow(ctag, std::complex<float>(1.0f)) == 4); |
| 77 | + assert(pow(std::complex<float>(1.0f), ctag) == 5); |
| 78 | + assert(pow(ctag, std::complex<double>(1.0)) == 4); |
| 79 | + assert(pow(std::complex<double>(1.0), ctag) == 5); |
| 80 | + assert(pow(ctag, std::complex<long double>(1.0l)) == 4); |
| 81 | + assert(pow(std::complex<long double>(1.0l), ctag) == 5); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
0 commit comments