Date: Mon, 29 Apr 2024 17:25:29 -0400
On 4/29/24 5:22 PM, Peter Dimov via SG16 wrote:
> Tom Honermann wrote:
>> There is, but it doesn't have good ergonomics, probably doesn't work for all
>> use cases, and gcc/libstdc++ currently reject. You have to declare a static
>> constexpr buffer. Clang with libc++ and MSVC accept the following.
>>
>> https://godbolt.org/z/5K8Pzo1Mr.
>>
>> #include <string>
>> #include <string_view>
>> consteval std::string f() {
>> return "test";
>> }
>> template<auto> void ft() {}
>> void g() {
>> static constexpr std::string s = f();
>> constexpr const char *p = s.data();
>> ft<p>();
>> constexpr std::string_view sv(s);
>> ft<sv.data()>();
>> }
> You need to use a longer string, one that doesn't fit into string's
> internal buffer and will go to the heap.
>
> https://godbolt.org/z/7K8WcTqbE
Bah, I forgot that matter. Thanks.
Tom.
> Tom Honermann wrote:
>> There is, but it doesn't have good ergonomics, probably doesn't work for all
>> use cases, and gcc/libstdc++ currently reject. You have to declare a static
>> constexpr buffer. Clang with libc++ and MSVC accept the following.
>>
>> https://godbolt.org/z/5K8Pzo1Mr.
>>
>> #include <string>
>> #include <string_view>
>> consteval std::string f() {
>> return "test";
>> }
>> template<auto> void ft() {}
>> void g() {
>> static constexpr std::string s = f();
>> constexpr const char *p = s.data();
>> ft<p>();
>> constexpr std::string_view sv(s);
>> ft<sv.data()>();
>> }
> You need to use a longer string, one that doesn't fit into string's
> internal buffer and will go to the heap.
>
> https://godbolt.org/z/7K8WcTqbE
Bah, I forgot that matter. Thanks.
Tom.
Received on 2024-04-29 21:25:32