C++ Logo

sg16

Advanced search

Re: Follow up on SG16 review of P2996R2 (Reflection for C++26)

From: Peter Dimov <pdimov_at_[hidden]>
Date: Tue, 30 Apr 2024 00:22:04 +0300
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

Received on 2024-04-29 21:22:09