Skip to content

Commit 0f8dbb2

Browse files
[libc++] Constrain additional overloads of pow for complex harder (#110235)
Fixes #109858. The changes in #81379 broke some 3rd party library code that expected usability of `std::complex<NonFloatingPoint>`. Although such code isn't portable per [complex.numbers.general]/2, it might be better to make these additional overloads not to interfere overload resolution too much. --------- Co-authored-by: Louis Dionne <[email protected]>
1 parent f71ea0e commit 0f8dbb2

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

libcxx/include/complex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,20 +1097,20 @@ inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const com
10971097
return std::exp(__y * std::log(__x));
10981098
}
10991099

1100-
template <class _Tp, class _Up>
1100+
template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
11011101
inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type>
11021102
pow(const complex<_Tp>& __x, const complex<_Up>& __y) {
11031103
typedef complex<typename __promote<_Tp, _Up>::type> result_type;
11041104
return std::pow(result_type(__x), result_type(__y));
11051105
}
11061106

1107-
template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Up>::value, int> = 0>
1107+
template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
11081108
inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const complex<_Tp>& __x, const _Up& __y) {
11091109
typedef complex<typename __promote<_Tp, _Up>::type> result_type;
11101110
return std::pow(result_type(__x), result_type(__y));
11111111
}
11121112

1113-
template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value, int> = 0>
1113+
template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
11141114
inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const _Tp& __x, const complex<_Up>& __y) {
11151115
typedef complex<typename __promote<_Tp, _Up>::type> result_type;
11161116
return std::pow(result_type(__x), result_type(__y));
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)