This adds constexpr to 11 algorithms defined in <numeric> as per P1645R1.
Tested on x86_64-pc-linux-gnu, OK to commit?
libstdc++-v3/ChangeLog:
P1645R1 constexpr for <numeric> algorithms
* include/bits/stl_numeric.h (iota, accumulate, inner_product,
partial_sum, adjacent_difference): Make conditionally constexpr for
C++20.
* include/std/numeric (reduce, transform_reduce, exclusive_scan,
inclusive_scan, transform_exclusive_scan, transform_inclusive_scan):
Likewise. Define the feature test macro __cpp_lib_constexpr_numeric.
* testsuite/26_numerics/accumulate/constexpr.cc: New test.
* testsuite/26_numerics/adjacent_difference/constexpr.cc: Likewise.
* testsuite/26_numerics/exclusive_scan/constexpr.cc: Likewise.
* testsuite/26_numerics/inclusive_scan/constexpr.cc: Likewise.
* testsuite/26_numerics/inner_product/constexpr.cc: Likewise.
* testsuite/26_numerics/iota/constexpr.cc: Likewise.
* testsuite/26_numerics/partial_sum/constexpr.cc: Likewise.
* testsuite/26_numerics/reduce/constexpr.cc: Likewise.
* testsuite/26_numerics/transform_exclusive_scan/constexpr.cc: Likewise.
* testsuite/26_numerics/transform_inclusive_scan/constexpr.cc: Likewise.
* testsuite/26_numerics/transform_reduce/constexpr.cc: Likewise.
---
libstdc++-v3/include/bits/stl_numeric.h | 9 +++
libstdc++-v3/include/std/numeric | 18 ++++++
.../26_numerics/accumulate/constexpr.cc | 42 ++++++++++++++
.../adjacent_difference/constexpr.cc | 44 +++++++++++++++
.../26_numerics/exclusive_scan/constexpr.cc | 44 +++++++++++++++
.../26_numerics/inclusive_scan/constexpr.cc | 55 +++++++++++++++++++
.../26_numerics/inner_product/constexpr.cc | 45 +++++++++++++++
.../testsuite/26_numerics/iota/constexpr.cc | 31 +++++++++++
.../26_numerics/partial_sum/constexpr.cc | 44 +++++++++++++++
.../testsuite/26_numerics/reduce/constexpr.cc | 52 ++++++++++++++++++
.../transform_exclusive_scan/constexpr.cc | 33 +++++++++++
.../transform_inclusive_scan/constexpr.cc | 44 +++++++++++++++
.../26_numerics/transform_reduce/constexpr.cc | 55 +++++++++++++++++++
13 files changed, 516 insertions(+)
create mode 100644 libstdc++-v3/testsuite/26_numerics/accumulate/constexpr.cc
create mode 100644
libstdc++-v3/testsuite/26_numerics/adjacent_difference/constexpr.cc
create mode 100644
libstdc++-v3/testsuite/26_numerics/exclusive_scan/constexpr.cc
create mode 100644
libstdc++-v3/testsuite/26_numerics/inclusive_scan/constexpr.cc
create mode 100644 libstdc++-v3/testsuite/26_numerics/inner_product/constexpr.cc
create mode 100644 libstdc++-v3/testsuite/26_numerics/iota/constexpr.cc
create mode 100644 libstdc++-v3/testsuite/26_numerics/partial_sum/constexpr.cc
create mode 100644 libstdc++-v3/testsuite/26_numerics/reduce/constexpr.cc
create mode 100644
libstdc++-v3/testsuite/26_numerics/transform_exclusive_scan/constexpr.cc
create mode 100644
libstdc++-v3/testsuite/26_numerics/transform_inclusive_scan/constexpr.cc
create mode 100644
libstdc++-v3/testsuite/26_numerics/transform_reduce/constexpr.cc
diff --git a/libstdc++-v3/include/bits/stl_numeric.h
b/libstdc++-v3/include/bits/stl_numeric.h
index dd4683fe92e..f95c86a0d48 100644
--- a/libstdc++-v3/include/bits/stl_numeric.h
+++ b/libstdc++-v3/include/bits/stl_numeric.h
@@ -83,6 +83,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @ingroup numeric_ops
*/
template<typename _ForwardIterator, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
void
iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
{
@@ -128,6 +129,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
* @return The final sum.
*/
template<typename _InputIterator, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
inline _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
{
@@ -154,6 +156,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
* @return The final sum.
*/
template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
+ _GLIBCXX20_CONSTEXPR
inline _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
_BinaryOperation __binary_op)
@@ -182,6 +185,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
* @return The final inner product.
*/
template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
inline _Tp
inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _Tp __init)
@@ -214,6 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
*/
template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
typename _BinaryOperation1, typename _BinaryOperation2>
+ _GLIBCXX20_CONSTEXPR
inline _Tp
inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _Tp __init,
@@ -246,6 +251,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
* @return Iterator pointing just beyond the values written to __result.
*/
template<typename _InputIterator, typename _OutputIterator>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
partial_sum(_InputIterator __first, _InputIterator __last,
_OutputIterator __result)
@@ -287,6 +293,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryOperation>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
partial_sum(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _BinaryOperation __binary_op)
@@ -326,6 +333,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
* DR 539. partial_sum and adjacent_difference should mention requirements
*/
template<typename _InputIterator, typename _OutputIterator>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
adjacent_difference(_InputIterator __first,
_InputIterator __last, _OutputIterator __result)
@@ -369,6 +377,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryOperation>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
adjacent_difference(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _BinaryOperation __binary_op)
diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric
index cf35191cb47..8a7830ffd85 100644
--- a/libstdc++-v3/include/std/numeric
+++ b/libstdc++-v3/include/std/numeric
@@ -249,6 +249,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* arithmetic) the result can be different.
*/
template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
+ _GLIBCXX20_CONSTEXPR
_Tp
reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
_BinaryOperation __binary_op)
@@ -284,6 +285,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* Equivalent to calling `std::reduce(first, last, init, std::plus<>())`.
*/
template<typename _InputIterator, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
inline _Tp
reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
{ return std::reduce(__first, __last, std::move(__init), plus<>()); }
@@ -300,6 +302,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* Equivalent to calling `std::reduce(first, last, T{}, std::plus<>())`.
*/
template<typename _InputIterator>
+ _GLIBCXX20_CONSTEXPR
inline typename iterator_traits<_InputIterator>::value_type
reduce(_InputIterator __first, _InputIterator __last)
{
@@ -327,6 +330,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
typename _BinaryOperation1, typename _BinaryOperation2>
+ _GLIBCXX20_CONSTEXPR
_Tp
transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _Tp __init,
@@ -369,6 +373,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* elements.
*/
template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
inline _Tp
transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _Tp __init)
@@ -394,6 +399,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator, typename _Tp,
typename _BinaryOperation, typename _UnaryOperation>
+ _GLIBCXX20_CONSTEXPR
_Tp
transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
_BinaryOperation __binary_op, _UnaryOperation __unary_op)
@@ -436,6 +442,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator, typename _OutputIterator, typename _Tp,
typename _BinaryOperation>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
exclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Tp __init,
@@ -469,6 +476,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* so the Nth input element is not included.
*/
template<typename _InputIterator, typename _OutputIterator, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
inline _OutputIterator
exclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Tp __init)
@@ -497,6 +505,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryOperation, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
inclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _BinaryOperation __binary_op,
@@ -525,6 +534,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryOperation>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
inclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _BinaryOperation __binary_op)
@@ -557,6 +567,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* so the Nth input element is included.
*/
template<typename _InputIterator, typename _OutputIterator>
+ _GLIBCXX20_CONSTEXPR
inline _OutputIterator
inclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result)
@@ -584,6 +595,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator, typename _OutputIterator, typename _Tp,
typename _BinaryOperation, typename _UnaryOperation>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Tp __init,
@@ -622,6 +634,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryOperation, typename _UnaryOperation, typename _Tp>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result,
@@ -655,6 +668,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryOperation, typename _UnaryOperation>
+ _GLIBCXX20_CONSTEXPR
_OutputIterator
transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
_OutputIterator __result,
@@ -676,6 +690,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// @} group numeric_ops
+#if __cplusplus > 201703L
+#define __cpp_lib_constexpr_numeric 201911L
+#endif