Bug 94498 - std::basic_string_view::copy not qualified constexpr in c++2a
Summary: std::basic_string_view::copy not qualified constexpr in c++2a
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 9.3.0
: P3 normal
Target Milestone: 10.0
Assignee: Jonathan Wakely
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2020-04-06 11:38 UTC by Takeshi Watanabe
Modified: 2020-08-07 10:26 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-04-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Takeshi Watanabe 2020-04-06 11:38:03 UTC
Following code fails to compile:
```c++
#include <iostream>
#include <fstream>
#include <string_view>

constexpr size_t copy(std::string_view str, char* dst) {
    return str.copy(dst, 1);
}

int main() {
    std::string_view str = "aaaa";
    char a[2];
    copy(str, a);
    std::cout << a[0] << std::endl;
}
```

with:

```
prog.cc: In function 'constexpr size_t copy(std::string_view, char*)':
prog.cc:6:20: error: call to non-'constexpr' function 'std::basic_string_view<_CharT, _Traits>::size_type std::basic_string_view<_CharT, _Traits>::copy(_CharT*, std::basic_string_view<_CharT, _Traits>::size_type, std::basic_string_view<_CharT, _Traits>::size_type) const [with _CharT = char; _Traits = std::char_traits<char>; std::basic_string_view<_CharT, _Traits>::size_type = long unsigned int]'
    6 |     return str.copy(dst, 1);
      |            ~~~~~~~~^~~~~~~~
```

https://wandbox.org/permlink/pNreqN8l5RPdPlTZ
Comment 1 GCC Commits 2020-04-06 17:31:07 UTC
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

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

commit r10-7569-gb696698767ba45b4d61a93205167e2f1f744d3f1
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Apr 6 18:30:53 2020 +0100

    libstdc++: Make string_view::copy usable in constant expressions (PR 94498)
    
            PR libstdc++/94498
            * include/bits/char_traits.h (__gnu_cxx::char_traits::move): Make it
            usable in constant expressions for C++20.
            (__gnu_cxx::char_traits::copy, __gnu_cxx::char_traits::assign): Add
            _GLIBCXX20_CONSTEXPR.
            (std::char_traits<char>, std::char_traits<wchar_t>)
            (std::char_traits<char8_t>): Make move, copy and assign usable in
            constant expressions for C++20.
            (std::char_traits<char16_t>, std::char_traits<char32_t>): Make move
            and copy usable in constant expressions for C++20.
            * include/std/string_view (basic_string_view::copy): Add
            _GLIBCXX20_CONSTEXPR.
            * testsuite/21_strings/basic_string_view/operations/copy/char/
            constexpr.cc: New test.
            * testsuite/21_strings/basic_string_view/operations/copy/wchar_t/
            constexpr.cc: New test.
Comment 2 Jonathan Wakely 2020-08-07 10:26:57 UTC
I was going to backport this to gcc-9 but it requires std::copy to be constexpr, and I do not plan to backport that.

So fixed for gcc-10.