constant expression context for `std::string_view`
Hans Åberg
haberg_1@icloud.com
Thu Dec 19 17:38:32 GMT 2024
The string_view object is created at runtime, for example this code passes:
#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 << std::endl;
return EXIT_SUCCESS;
}
> On 19 Dec 2024, at 15:34, Yu Sheng Oh via Gcc-help <gcc-help@gcc.gnu.org> wrote:
>
> Hi guys,
>
> May I know why the compilation failed for the following code where the
> compiler is complaining that `s` is not a constant expression? While
> removing the `static_assert`, the compilation passed. Regards.
>
> 1 #include <array>
> 2 #include <string_view>
> 3 #include <cstddef>
> 4
> 5 template<std::size_t N>
> 6 constexpr std::array<int, N> test(std::string_view s)
> 7 {
> 8 static_assert(s.size() > N);
> 9 return { 0 };
> 10 }
> 11
> 12 int main(int argc, char *argv[])
> 13 {
> 14 using namespace std::literals;
> 15 constexpr auto s = "123"sv;
> 16 constexpr auto a = test<2>(s);
> 17 return 0;
> 18 }
More information about the Gcc-help
mailing list