public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly
@ 2021-05-27 10:07 hewillk at gmail dot com
  2021-05-27 10:17 ` [Bug libstdc++/100795] " redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: hewillk at gmail dot com @ 2021-05-27 10:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100795
           Summary: ranges::sample should not use std::sample directly
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

Since some random access ranges are only input ranges in C++17, this will cause
ranges::sample to incorrectly reject the following valid case:


#include <algorithm>
#include <iostream>
#include <random>
#include <ranges>

int main() {
  std::ranges::sample(
    std::views::iota(0, 42),
    std::ostream_iterator<int>{std::cout, " "},
    42,
    std::mt19937{std::random_device{}()});
}

https://godbolt.org/z/dbWWTPKhx

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
@ 2021-05-27 10:17 ` redi at gcc dot gnu.org
  2021-05-27 11:55 ` hewillk at gmail dot com
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-27 10:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-05-27
     Ever confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This works though:

--- include/bits/ranges_algo.h
+++ include/bits/ranges_algo.h
@@ -1762,8 +1762,10 @@
            // which may take linear time.
            auto __lasti = ranges::next(__first, __last);
            return _GLIBCXX_STD_A::
-             sample(std::move(__first), std::move(__lasti), std::move(__out),
-                    __n, std::forward<_Gen>(__g));
+             __sample(std::move(__first), std::move(__lasti),
+                      forward_iterator_tag{},
+                      std::move(__out), 0,
+                      __n, std::forward<_Gen>(__g));
          }
        else
          {

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
  2021-05-27 10:17 ` [Bug libstdc++/100795] " redi at gcc dot gnu.org
@ 2021-05-27 11:55 ` hewillk at gmail dot com
  2021-05-27 12:11 ` hewillk at gmail dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: hewillk at gmail dot com @ 2021-05-27 11:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from 康桓瑋 <hewillk at gmail dot com> ---
ranges::inplace_merge has the same issue:


#include <algorithm>
#include <array>
#include <ranges>

int main() {
  std::array a{42, 42, 42};
  auto r = std::views::iota(0, 3) | 
           std::views::transform([&a](int i) -> int& { return a[i]; });
  std::ranges::inplace_merge(r, r.begin());
}

https://godbolt.org/z/q6jbxGMjd

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
  2021-05-27 10:17 ` [Bug libstdc++/100795] " redi at gcc dot gnu.org
  2021-05-27 11:55 ` hewillk at gmail dot com
@ 2021-05-27 12:11 ` hewillk at gmail dot com
  2021-05-27 14:53 ` hewillk at gmail dot com
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: hewillk at gmail dot com @ 2021-05-27 12:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from 康桓瑋 <hewillk at gmail dot com> ---
(In reply to 康桓瑋 from comment #2)
> ranges::inplace_merge has the same issue:
> 
> 
> #include <algorithm>
> #include <array>
> #include <ranges>
> 
> int main() {
>   std::array a{42, 42, 42};
>   auto r = std::views::iota(0, 3) | 
>            std::views::transform([&a](int i) -> int& { return a[i]; });
>   std::ranges::inplace_merge(r, r.begin());
> }
> 
> https://godbolt.org/z/q6jbxGMjd

Same for ranges::stable_sort and ranges::stable_partition.
(https://godbolt.org/z/qffGMW477)

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
                   ` (2 preceding siblings ...)
  2021-05-27 12:11 ` hewillk at gmail dot com
@ 2021-05-27 14:53 ` hewillk at gmail dot com
  2021-05-27 14:58 ` hewillk at gmail dot com
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: hewillk at gmail dot com @ 2021-05-27 14:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from 康桓瑋 <hewillk at gmail dot com> ---
Same with ranges::make_heap, ranges::push_heap, ranges::pop_heap, and
ranges::sort_heap when _GLIBCXX_CONCEPT_CHECKS is defined.

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
                   ` (3 preceding siblings ...)
  2021-05-27 14:53 ` hewillk at gmail dot com
@ 2021-05-27 14:58 ` hewillk at gmail dot com
  2021-07-16 16:01 ` ppalka at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: hewillk at gmail dot com @ 2021-05-27 14:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from 康桓瑋 <hewillk at gmail dot com> ---
(In reply to 康桓瑋 from comment #4)
> Same with ranges::make_heap, ranges::push_heap, ranges::pop_heap, and
> ranges::sort_heap when _GLIBCXX_CONCEPT_CHECKS is defined.

And std::ranges::shuffle.

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
                   ` (4 preceding siblings ...)
  2021-05-27 14:58 ` hewillk at gmail dot com
@ 2021-07-16 16:01 ` ppalka at gcc dot gnu.org
  2021-07-16 16:22 ` hewillk at gmail dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-07-16 16:01 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to 康桓瑋 from comment #2)
> ranges::inplace_merge has the same issue:
> 
> 
> #include <algorithm>
> #include <array>
> #include <ranges>
> 
> int main() {
>   std::array a{42, 42, 42};
>   auto r = std::views::iota(0, 3) | 
>            std::views::transform([&a](int i) -> int& { return a[i]; });
>   std::ranges::inplace_merge(r, r.begin());
> }
> 
> https://godbolt.org/z/q6jbxGMjd

Hmm, this one seems more like a problem in the specification of
transform_view::iterator_category.  Shouldn't r's iterator_category be
random_access_iterator here?  AFAICT it satisfies all the requirements of
__LegacyRandomAccessIterator.

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
                   ` (5 preceding siblings ...)
  2021-07-16 16:01 ` ppalka at gcc dot gnu.org
@ 2021-07-16 16:22 ` hewillk at gmail dot com
  2021-11-23 16:54 ` barry.revzin at gmail dot com
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: hewillk at gmail dot com @ 2021-07-16 16:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from 康桓瑋 <hewillk at gmail dot com> ---
(In reply to Patrick Palka from comment #6)
> (In reply to 康桓瑋 from comment #2)
> > ranges::inplace_merge has the same issue:
> > 
> > 
> > #include <algorithm>
> > #include <array>
> > #include <ranges>
> > 
> > int main() {
> >   std::array a{42, 42, 42};
> >   auto r = std::views::iota(0, 3) | 
> >            std::views::transform([&a](int i) -> int& { return a[i]; });
> >   std::ranges::inplace_merge(r, r.begin());
> > }
> > 
> > https://godbolt.org/z/q6jbxGMjd
> 
> Hmm, this one seems more like a problem in the specification of
> transform_view::iterator_category.  Shouldn't r's iterator_category be
> random_access_iterator here?  AFAICT it satisfies all the requirements of
> __LegacyRandomAccessIterator.

Damn, I think you are right. According to [range.transform.iterator#2.1], even
if is_lvalue_reference_v<invoke_result_t<F&, range_reference_t<Base>>> is true,
transform_view::iterator_category is still determined by the
iota_view::iterator_category.

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
                   ` (6 preceding siblings ...)
  2021-07-16 16:22 ` hewillk at gmail dot com
@ 2021-11-23 16:54 ` barry.revzin at gmail dot com
  2022-02-16  0:44 ` pinskia at gcc dot gnu.org
  2022-07-20  6:30 ` hewillk at gmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: barry.revzin at gmail dot com @ 2021-11-23 16:54 UTC (permalink / raw)
  To: gcc-bugs

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

Barry Revzin <barry.revzin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |barry.revzin at gmail dot com

--- Comment #8 from Barry Revzin <barry.revzin at gmail dot com> ---
Related: https://stackoverflow.com/q/70076645/2069064

std::ranges::sort calls std::sort, which then uses std::iter_swap instead of
std::ranges::iter_swap.

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
                   ` (7 preceding siblings ...)
  2021-11-23 16:54 ` barry.revzin at gmail dot com
@ 2022-02-16  0:44 ` pinskia at gcc dot gnu.org
  2022-07-20  6:30 ` hewillk at gmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-16  0:44 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Berenger.Berthoul at onera dot fr

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 104561 has been marked as a duplicate of this bug. ***

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

* [Bug libstdc++/100795] ranges::sample should not use std::sample directly
  2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
                   ` (8 preceding siblings ...)
  2022-02-16  0:44 ` pinskia at gcc dot gnu.org
@ 2022-07-20  6:30 ` hewillk at gmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: hewillk at gmail dot com @ 2022-07-20  6:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from 康桓瑋 <hewillk at gmail dot com> ---
For non-std::sample branches, we still have to consider that _Out only works
with its difference_type, so an explicit casting is missing here.

1580 |             return __out + __sample_sz;
     |                    ~~~~~~^~~~~~~~~~~~~

https://godbolt.org/z/T4s99GMrP

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

end of thread, other threads:[~2022-07-20  6:30 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-27 10:07 [Bug libstdc++/100795] New: ranges::sample should not use std::sample directly hewillk at gmail dot com
2021-05-27 10:17 ` [Bug libstdc++/100795] " redi at gcc dot gnu.org
2021-05-27 11:55 ` hewillk at gmail dot com
2021-05-27 12:11 ` hewillk at gmail dot com
2021-05-27 14:53 ` hewillk at gmail dot com
2021-05-27 14:58 ` hewillk at gmail dot com
2021-07-16 16:01 ` ppalka at gcc dot gnu.org
2021-07-16 16:22 ` hewillk at gmail dot com
2021-11-23 16:54 ` barry.revzin at gmail dot com
2022-02-16  0:44 ` pinskia at gcc dot gnu.org
2022-07-20  6:30 ` hewillk at gmail 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).