<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/67046>67046</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
C++20 std::midpoint is non-conformant
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bregma
</td>
</tr>
</table>
<pre>
The second (pointer) overload of `std::midpoint()` fails to compile when used explicitly. This is a failure to accept valid code according to ISO/IEC 14882:2020 25.10.16/4 [numeric.ops.midpoint].
Example:
```
#include <numeric>
template<typename T>
T* mp(T* b, T*e) {
return std::midpoint<T>(b, e);
}
int main() {
char a[2];
char* p = mp(a, a+2);
}
```
gives the following compilation errors.
```
<source>:5:12: error: no matching function for call to 'midpoint'
5 | return std::midpoint<T>(b, e);
| ^~~~~~~~~~~~~~~~
<source>:10:15: note: in instantiation of function template specialization 'mp<char>' requested here
10 | char* p = mp(a, a+2);
| ^
/opt/compiler-explorer/clang-17.0.1/bin/../include/c++/v1/__numeric/midpoint.h:40:1: note: candidate function template not viable: no known conversion from 'char *' to 'char' for 1st argument; dereference the argument with *
40 | midpoint(_Tp __a, _Tp __b) noexcept
| ^ ~~~~~~~
/opt/compiler-explorer/clang-17.0.1/bin/../include/c++/v1/__numeric/midpoint.h:61:1: note: candidate template ignored: requirement 'is_pointer_v<char>' was not satisfied [with _TPtr = char]
61 | midpoint(_TPtr __a, _TPtr __b) noexcept
| ^
/opt/compiler-explorer/clang-17.0.1/bin/../include/c++/v1/__numeric/midpoint.h:78:1: note: candidate template ignored: requirement 'is_floating_point_v<char>' was not satisfied [with _Fp = char]
78 | midpoint(_Fp __a, _Fp __b) noexcept
| ^
1 error generated.
Compiler returned: 1
```
The following patch resolves the error.
```
57c57,58
< enable_if_t<is_object_v<remove_pointer_t<_TPtr>>
---
> enable_if_t<is_pointer_v<_TPtr>
> && is_object_v<remove_pointer_t<_TPtr>>
59,60c60,61
< && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr*>
< midpoint(_TPtr* __a, _TPtr* __b) noexcept
---
> && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
> midpoint(_TPtr __a, _TPtr __b) noexcept
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8lk_P4rYTx1-NuYyIEof84cCBB55Ie_r9pHJHxhmI28RObYdndw997dU4EEDQdndbbYRI4ozHMx9_ZxLhnDppxBXL3li2nYnBN8auDhZPnZgdTP1ltWsQHEqja2C87I3SHi3jSzBntK0RNZgjsDx2vmbpmqXrTtXBivGS8SXLYzgK1TrwBqTpetUifDSoYXBYA37uWyWVb79EsGuUA-VAhAmDRZoipMTew1m0qgZpaqQRY2ulT_T40y__Y7z69L6BZFGWnKVrHvMYeBYlcZTkjFcLYNmbHjq0Skamd9EUX7aNWLxl8Xr8f_8sur5FSmIczePLb7zlqdKyHWoElm4uDln6fu_CY9e3wiNLN_5Lj1p0CLvJBmDH-Bq6nvEyXB0Y34QxJJ6seLvaAVj0g9XwDDXdBIe8DJNpIkuneazY3oejtIdOKD3uxOMCshEWBMveOMu2Nxc0TKH1wNLtGKqghQTjb_x-sdtSj5hO6owOfINwNG1rPmifxm0XXhkNaK2xLnqNON04M1iJlGG6zli6TmhLx0l0oQ10wsuGvB4HLYPLo7EgRduSHhgvbvorbulmwIoN_DhZOsgDy97_eDxehp7E9JeNIZMe1qA0KO280F6NJMzxlsJVOOB6lEq06utoQ-n0LN2EbaHgCrD4-4DOYw0NWpziS-Ipw2_ew4e0riqvTO8Zry6laudUocZSyVeyFfo0T4oojhLGqwMpq4oixqtLZZAN42_hV53JZr-_VgqvrqSjhqXrRSB0D0gKXauaIDxj0cbDWYlDKE9SwW_afGiQRp_RuiACazrCFXTN-JpQjXoYcRRBJonzIOxp6JA2_A1qtHhEi1pi0Oz1GXwo3wQ3V1CLke9dc9vvetjvA9nx8kBFpg1-po71gjA8KuZnkc6TvyY9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