The following use of constexpr std::string_view produces "format not a string literal" warning:
constexpr std::string_view string_view_format_str = "hello %s";
snprintf(string_view_warning, 100, string_view_format_str.data(), "world");
warning: format not a string literal, argument types not checked [-Wformat-nonliteral]
And doing the following afterwards, doesn't:
constexpr const char * const_char_format = string_view_format_str.data();
snprintf(string_view_warning, 100, const_char_format, "world");
Why constexpr string_view produces that warning?
https://godbolt.org/z/hT6xqhGeW
Both GCC and clang give the same result:
- GCC (trunk) with -Wformat, -Wformat-nonliteral
- clang (> 5.0.0) with -Weverything
The warning isn't for
string_view
, it's forsnprintf
. That's a C function, and there's noconstexpr
in C, so it makes sense that GCC didn't bother.