I can see that size() is indeed marked constexpr here: https://en.cppreference.com/w/cpp/string/basic_string_view/size But if I modify my code to the one below, it compiles and I can pass program arguments of different sizes which it writes out, and which are not known at compile time. #include <stdlib.h> #include <iostream> #include <string_view> #include <vector> int main(int argc, char* argv[], char* envp[]) { std::vector<std::string_view> args(argv, argv + argc); for (auto& i: args) std::cout << i << ": " << i.size() << std::endl; return EXIT_SUCCESS; } > On 19 Dec 2024, at 19:33, Yu Sheng Oh via Gcc-help <gcc-help@xxxxxxxxxxx> wrote: > > ---------- Forwarded message --------- > From: Yu Sheng Oh <humbleoh@xxxxxxxxx> > Date: Fri, Dec 20, 2024 at 2:30 AM > Subject: Re: constant expression context for `std::string_view` > To: Hans Åberg <haberg_1@xxxxxxxxxx> > > > referring to my previous code, line 16 is a constant expression. if > `static_assert` at line 8 is removed, then the compilation would > succeed. if `string_view` object is created at runtime, the compiler > should complain about line 16 when line 8 is removed. giving another > simple test code in the following, which passes the compilation, if > line 14 is uncommented, the compilation will fail again. i just doubt > if it is a bug in the compiler or i have missed something about the > constant expression. regards.