public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor
@ 2020-06-23  7:20 eugns.p at gmail dot com
  2021-05-15  8:56 ` [Bug libstdc++/95833] " TonyELewis at hotmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: eugns.p at gmail dot com @ 2020-06-23  7:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

            Bug ID: 95833
           Summary: Incorrect static_assert in std::reduce overload taking
                    a binary functor
           Product: gcc
           Version: 10.0
               URL: https://stackoverflow.com/q/62499765
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eugns.p at gmail dot com
  Target Milestone: ---

Created attachment 48773
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48773&action=edit
Preprocessed code for minimal example

The std::reduce overload that takes a binary functor

template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
_Tp reduce(_InputIterator, _InputIterator, _Tp, _BinaryOperation);

(defined here:
https://github.com/gcc-mirror/gcc/blob/releases/gcc-10/libstdc%2B%2B-v3/include/std/numeric#L236)

contains an incorrect static assertion:

static_assert(is_convertible_v<value_type, _Tp>);

There is no such requirement in the standard. This static assert causes a
compilation failure of the following minimal example:

#include <iostream>
#include <array>
#include <numeric>
#include <cstring>

struct StringLength {
    auto operator()(const char* l, std::size_t r) const {
        return std::strlen(l) + r;
    }

    auto operator()(std::size_t l, const char* r) const {
        return l + std::strlen(r);
    }

    auto operator()(const char* l, const char* r) const {
        return std::strlen(l) + std::strlen(r);
    }

    auto operator()(std::size_t l, std::size_t r) const {
        return l + r;
    }
};

int main() {
    std::array<const char*, 3> arr{"A", "B", "C"};
    std::cout << std::reduce(arr.begin(), arr.end(), std::size_t{},
StringLength{});
}

https://godbolt.org/z/Av3iY9

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
@ 2021-05-15  8:56 ` TonyELewis at hotmail dot com
  2021-05-15 21:17 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: TonyELewis at hotmail dot com @ 2021-05-15  8:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

Tony E Lewis <TonyELewis at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |TonyELewis at hotmail dot com

--- Comment #1 from Tony E Lewis <TonyELewis at hotmail dot com> ---
I agree with the OP's report: I think this static_assert() is rejecting valid
calls and accepting invalid calls.

AFAIU from https://eel.is/c++draft/reduce, we don't require that *the range's
value type* is convertible to the type we're reducing to; we require that *the
binary operation always returns something* that's convertible to the type being
reduced to.

So I think these two lines in the numeric header:

static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
static_assert(is_convertible_v<value_type, _Tp>);

...should instead be something like:

static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, value_type&>);
static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, value_type&, _Tp&>);
static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, value_type&,
value_type&>);

(location :
https://github.com/gcc-mirror/gcc/blob/af42043e6618e69187b47f37dac870763c01e20f/libstdc%2B%2B-v3/include/std/numeric#L282
)

Thanks very much for all work on libstdc++.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
  2021-05-15  8:56 ` [Bug libstdc++/95833] " TonyELewis at hotmail dot com
@ 2021-05-15 21:17 ` redi at gcc dot gnu.org
  2021-05-16  8:41 ` TonyELewis at hotmail dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-15 21:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Somehow I missed this when the report was originally filed. I'll take a look.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
  2021-05-15  8:56 ` [Bug libstdc++/95833] " TonyELewis at hotmail dot com
  2021-05-15 21:17 ` redi at gcc dot gnu.org
@ 2021-05-16  8:41 ` TonyELewis at hotmail dot com
  2021-06-18 13:50 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: TonyELewis at hotmail dot com @ 2021-05-16  8:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

--- Comment #3 from Tony E Lewis <TonyELewis at hotmail dot com> ---
Great. Thanks very much.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
                   ` (2 preceding siblings ...)
  2021-05-16  8:41 ` TonyELewis at hotmail dot com
@ 2021-06-18 13:50 ` redi at gcc dot gnu.org
  2021-06-18 14:06 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-06-18 13:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |9.5
   Last reconfirmed|                            |2021-06-18
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
                   ` (3 preceding siblings ...)
  2021-06-18 13:50 ` redi at gcc dot gnu.org
@ 2021-06-18 14:06 ` cvs-commit at gcc dot gnu.org
  2021-06-18 15:55 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-18 14:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:0532452dcd17910dfd3d2b0df50dfe3ef1194bf7

commit r12-1645-g0532452dcd17910dfd3d2b0df50dfe3ef1194bf7
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Jun 18 14:46:58 2021 +0100

    libstdc++: Replace incorrect static assertion in std::reduce [PR95833]

    The standard does not require the iterator's value type to be
    convertible to the result type, it only requires that the result of
    dereferencing the iterator can be passed to the binary function.

    libstdc++-v3/ChangeLog:

            PR libstdc++/95833
            * include/std/numeric (reduce(Iter, Iter, T, BinaryOp)): Replace
            incorrect static_assert with ones matching the 'Mandates'
            conditions in the standard.
            * testsuite/26_numerics/reduce/95833.cc: New test.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
                   ` (4 preceding siblings ...)
  2021-06-18 14:06 ` cvs-commit at gcc dot gnu.org
@ 2021-06-18 15:55 ` cvs-commit at gcc dot gnu.org
  2021-06-18 17:46 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-18 15:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:fe918041ce9809755a7e1b2f62071c2b27829610

commit r11-8621-gfe918041ce9809755a7e1b2f62071c2b27829610
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Jun 18 14:46:58 2021 +0100

    libstdc++: Replace incorrect static assertion in std::reduce [PR95833]

    The standard does not require the iterator's value type to be
    convertible to the result type, it only requires that the result of
    dereferencing the iterator can be passed to the binary function.

    libstdc++-v3/ChangeLog:

            PR libstdc++/95833
            * include/std/numeric (reduce(Iter, Iter, T, BinaryOp)): Replace
            incorrect static_assert with ones matching the 'Mandates'
            conditions in the standard.
            * testsuite/26_numerics/reduce/95833.cc: New test.

    (cherry picked from commit 0532452dcd17910dfd3d2b0df50dfe3ef1194bf7)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
                   ` (5 preceding siblings ...)
  2021-06-18 15:55 ` cvs-commit at gcc dot gnu.org
@ 2021-06-18 17:46 ` cvs-commit at gcc dot gnu.org
  2021-06-18 20:57 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-18 17:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:3a37d5c0bd3451104ddab85f69ec37b50d31fa7f

commit r10-9945-g3a37d5c0bd3451104ddab85f69ec37b50d31fa7f
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Jun 18 14:46:58 2021 +0100

    libstdc++: Replace incorrect static assertion in std::reduce [PR95833]

    The standard does not require the iterator's value type to be
    convertible to the result type, it only requires that the result of
    dereferencing the iterator can be passed to the binary function.

    libstdc++-v3/ChangeLog:

            PR libstdc++/95833
            * include/std/numeric (reduce(Iter, Iter, T, BinaryOp)): Replace
            incorrect static_assert with ones matching the 'Mandates'
            conditions in the standard.
            * testsuite/26_numerics/reduce/95833.cc: New test.

    (cherry picked from commit 0532452dcd17910dfd3d2b0df50dfe3ef1194bf7)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
                   ` (6 preceding siblings ...)
  2021-06-18 17:46 ` cvs-commit at gcc dot gnu.org
@ 2021-06-18 20:57 ` cvs-commit at gcc dot gnu.org
  2021-06-18 20:58 ` redi at gcc dot gnu.org
  2021-06-19  7:29 ` TonyELewis at hotmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-18 20:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:ae17634d5fce496a50477eb0199f1eda8249622f

commit r9-9594-gae17634d5fce496a50477eb0199f1eda8249622f
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Jun 18 14:46:58 2021 +0100

    libstdc++: Replace incorrect static assertion in std::reduce [PR95833]

    The standard does not require the iterator's value type to be
    convertible to the result type, it only requires that the result of
    dereferencing the iterator can be passed to the binary function.

    libstdc++-v3/ChangeLog:

            PR libstdc++/95833
            * include/std/numeric (reduce(Iter, Iter, T, BinaryOp)): Replace
            incorrect static_assert with ones matching the 'Mandates'
            conditions in the standard.
            * testsuite/26_numerics/reduce/95833.cc: New test.

    (cherry picked from commit 0532452dcd17910dfd3d2b0df50dfe3ef1194bf7)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
                   ` (7 preceding siblings ...)
  2021-06-18 20:57 ` cvs-commit at gcc dot gnu.org
@ 2021-06-18 20:58 ` redi at gcc dot gnu.org
  2021-06-19  7:29 ` TonyELewis at hotmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-06-18 20:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 9.5, 10.4 and 11.2

Thanks for the report.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug libstdc++/95833] Incorrect static_assert in std::reduce overload taking a binary functor
  2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
                   ` (8 preceding siblings ...)
  2021-06-18 20:58 ` redi at gcc dot gnu.org
@ 2021-06-19  7:29 ` TonyELewis at hotmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: TonyELewis at hotmail dot com @ 2021-06-19  7:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95833

--- Comment #9 from Tony E Lewis <TonyELewis at hotmail dot com> ---
Great. I confirm my original example code now compiles and runs cleanly on
Compiler Explorer. Thanks very much for this.

And thanks to OP for the report.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-06-19  7:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-23  7:20 [Bug libstdc++/95833] New: Incorrect static_assert in std::reduce overload taking a binary functor eugns.p at gmail dot com
2021-05-15  8:56 ` [Bug libstdc++/95833] " TonyELewis at hotmail dot com
2021-05-15 21:17 ` redi at gcc dot gnu.org
2021-05-16  8:41 ` TonyELewis at hotmail dot com
2021-06-18 13:50 ` redi at gcc dot gnu.org
2021-06-18 14:06 ` cvs-commit at gcc dot gnu.org
2021-06-18 15:55 ` cvs-commit at gcc dot gnu.org
2021-06-18 17:46 ` cvs-commit at gcc dot gnu.org
2021-06-18 20:57 ` cvs-commit at gcc dot gnu.org
2021-06-18 20:58 ` redi at gcc dot gnu.org
2021-06-19  7:29 ` TonyELewis at hotmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).