Bug 100675 - std::string_view::find in constexpr function is not constant expression
Summary: std::string_view::find in constexpr function is not constant expression
Status: RESOLVED DUPLICATE of bug 97051
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.3.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2021-05-19 10:46 UTC by 康桓瑋
Modified: 2021-05-20 14:22 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description 康桓瑋 2021-05-19 10:46:54 UTC
The following code fails in 10.1, 10.2, and 10.3:

https://godbolt.org/z/soGT9o4GY

#include <string_view>

template <class>
constexpr bool g() {
  std::string_view s = " ";
  s.find(" ");
  return true;
}

constexpr bool b = g<void>();          // ok
template <class T> concept C = g<T>();
static_assert(C<void>);                // error
Comment 1 康桓瑋 2021-05-19 13:05:07 UTC
(In reply to 康桓瑋 from comment #0)
> The following code fails in 10.1, 10.2, and 10.3:
> 
> https://godbolt.org/z/soGT9o4GY
> 
> #include <string_view>
> 
> template <class>
> constexpr bool g() {
>   std::string_view s = " ";
>   s.find(" ");
>   return true;
> }
> 
> constexpr bool b = g<void>();          // ok
> template <class T> concept C = g<T>();
> static_assert(C<void>);                // error

If change constexpr to consteval then compiles:

template <class>
consteval bool g() {
  std::string_view s = " ";
  s.find(" ");
  return true;
}
Comment 2 Jonathan Wakely 2021-05-20 12:03:35 UTC
Maybe we should just get rid of the __constant_string_p stuff entirely.
Comment 3 Patrick Palka 2021-05-20 14:22:22 UTC
Already fixed on trunk by r11-3295 (and also fixed for 10.4).  This seems like ultimately a variant of PR97051, with the is_constant_evaluated() call instead deeply nested.

*** This bug has been marked as a duplicate of bug 97051 ***