public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b)
@ 2022-02-09 12:40 zyn7109 at gmail dot com
  2022-02-09 12:43 ` [Bug libstdc++/104465] " zyn7109 at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: zyn7109 at gmail dot com @ 2022-02-09 12:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104465
           Summary: std::vector<std::string> should satisfy
                    std::ranges::viewable_range (P2415 for -c++2b)
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zyn7109 at gmail dot com
  Target Milestone: ---

Given the following code,

```cpp
#include <iostream>
#include <ranges>
#include <string>
#include <vector>

int foo(std::ranges::viewable_range auto) { return 1; } // #1
int foo(auto) { return 0; }  // #2

int main() {
  const std::vector<std::string> v {"1", "2", "3", "4"};
  std::cout << foo(v) << " ";
  for (auto vv: v | std::views::reverse) std::cout << vv << " ";   // #3
}
```

compiling with GCC 12.0.1 (20220208)
(https://wandbox.org/permlink/aPZzDSVF0wbtUQF8), the output is "0 4 3 2 1"
rather than "1 4 3 2 1" since the overload resolution selects #2 for `foo`.
However, as P2415 was merged into C++23
(https://github.com/cplusplus/papers/issues/1085), `std::vector<std::string>`
is now a `viewable_range` and with Clang 15, "1 4 3 2 1" is the result.
(https://wandbox.org/permlink/8uzBa0Ot6Yj22Z0l).

It seems that P2415 is still not implemented for libstdc++ yet. However, GCC
accepts #3 since range was introduced from GCC 10.x (I'm not quite sure which
version) while considering
`std::ranges::viewable_range<std::vector<std::string>> == false`. As the
cppreference (https://en.cppreference.com/w/cpp/ranges/reverse_view) or C++20
draft suggests (24.7.1 [range.adaptor.object]/1,
https://timsong-cpp.github.io/cppwp/n4861/range.adaptors#range.adaptor.object-1),
the range adaptor `std::views::reverse` only takes `viewable_range` as
arguments. So it makes me confused and I wonder if I missed something.

Clang 13 rejects the above code (https://gcc.godbolt.org/z/xoKesEz7q), which is
as expected.

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

* [Bug libstdc++/104465] std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b)
  2022-02-09 12:40 [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b) zyn7109 at gmail dot com
@ 2022-02-09 12:43 ` zyn7109 at gmail dot com
  2022-02-09 13:16 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: zyn7109 at gmail dot com @ 2022-02-09 12:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Younan Zhang <zyn7109 at gmail dot com> ---
* link typo for Clang 13 demo: https://gcc.godbolt.org/z/f7Eb53zb5

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

* [Bug libstdc++/104465] std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b)
  2022-02-09 12:40 [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b) zyn7109 at gmail dot com
  2022-02-09 12:43 ` [Bug libstdc++/104465] " zyn7109 at gmail dot com
@ 2022-02-09 13:16 ` redi at gcc dot gnu.org
  2022-02-09 13:22 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-09 13:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Younan Zhang from comment #0)
> It seems that P2415 is still not implemented for libstdc++ yet.

Correct.

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

* [Bug libstdc++/104465] std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b)
  2022-02-09 12:40 [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b) zyn7109 at gmail dot com
  2022-02-09 12:43 ` [Bug libstdc++/104465] " zyn7109 at gmail dot com
  2022-02-09 13:16 ` redi at gcc dot gnu.org
@ 2022-02-09 13:22 ` redi at gcc dot gnu.org
  2022-02-09 13:24 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-09 13:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Younan Zhang from comment #0)
> cppreference (https://en.cppreference.com/w/cpp/ranges/reverse_view) or
> C++20 draft suggests (24.7.1 [range.adaptor.object]/1,
> https://timsong-cpp.github.io/cppwp/n4861/range.adaptors#range.adaptor.
> object-1), the range adaptor `std::views::reverse` only takes
> `viewable_range` as arguments. So it makes me confused and I wonder if I
> missed something.

[range.adaptor.object] says what happens if you use a viewable_range argument,
but it doesn't say what happens if you use something else. Specifically, it
doesn't say it's ill-formed.

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

* [Bug libstdc++/104465] std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b)
  2022-02-09 12:40 [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b) zyn7109 at gmail dot com
                   ` (2 preceding siblings ...)
  2022-02-09 13:22 ` redi at gcc dot gnu.org
@ 2022-02-09 13:24 ` redi at gcc dot gnu.org
  2022-02-09 13:35 ` redi at gcc dot gnu.org
  2022-02-09 14:55 ` zyn7109 at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-09 13:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
But the reason that it accepts the argument is that it's an lvalue.

You tested std::ranges::viewable_range<std::vector<std::string>> but you should
have tested std::ranges::viewable_range<std::vector<std::string>&>.

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

* [Bug libstdc++/104465] std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b)
  2022-02-09 12:40 [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b) zyn7109 at gmail dot com
                   ` (3 preceding siblings ...)
  2022-02-09 13:24 ` redi at gcc dot gnu.org
@ 2022-02-09 13:35 ` redi at gcc dot gnu.org
  2022-02-09 14:55 ` zyn7109 at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-09 13:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Younan Zhang from comment #0)
> Clang 13 rejects the above code (https://gcc.godbolt.org/z/xoKesEz7q), which
> is as expected.

That's just because Clang is broken and doesn't work with any libstdc++ views:
https://github.com/llvm/llvm-project/issues/44178

So there are several things going on here. You're correct that P2415 is not
implemented, but I think your example is supposed to compile even before that
change. An lvalue vector has always been a viewable range. Clang 13 rejects
your example because it's broken, not because of the viewable_range
requirement.

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

* [Bug libstdc++/104465] std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b)
  2022-02-09 12:40 [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b) zyn7109 at gmail dot com
                   ` (4 preceding siblings ...)
  2022-02-09 13:35 ` redi at gcc dot gnu.org
@ 2022-02-09 14:55 ` zyn7109 at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: zyn7109 at gmail dot com @ 2022-02-09 14:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Younan Zhang <zyn7109 at gmail dot com> ---
(In reply to Jonathan Wakely from comment #4)
> But the reason that it accepts the argument is that it's an lvalue.
> 
> You tested std::ranges::viewable_range<std::vector<std::string>> but you
> should have tested std::ranges::viewable_range<std::vector<std::string>&>.


(In reply to Jonathan Wakely from comment #5)
> An lvalue vector has always been a viewable range. 

Yes you're right. `foo(v)` will return 1 now:

```cpp
template <typename T>
int foo(T) requires std::ranges::viewable_range<T&> { return 1; }
int foo(auto) { return 0; }
```

I also tested `auto(v) | std::views::reverse` and GCC rejects with:

```
...required for the satisfaction of 'viewable_range<_Range>':
note: no operand of the disjunction is satisfied...
```


(In reply to Jonathan Wakely from comment #5)
> That's just because Clang is broken and doesn't work with any libstdc++ views:
https://github.com/llvm/llvm-project/issues/44178

looks subtle ;)

Thanks for your detailed answer~

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

end of thread, other threads:[~2022-02-09 14:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-09 12:40 [Bug libstdc++/104465] New: std::vector<std::string> should satisfy std::ranges::viewable_range (P2415 for -c++2b) zyn7109 at gmail dot com
2022-02-09 12:43 ` [Bug libstdc++/104465] " zyn7109 at gmail dot com
2022-02-09 13:16 ` redi at gcc dot gnu.org
2022-02-09 13:22 ` redi at gcc dot gnu.org
2022-02-09 13:24 ` redi at gcc dot gnu.org
2022-02-09 13:35 ` redi at gcc dot gnu.org
2022-02-09 14:55 ` zyn7109 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).