checking string-literal function argument at compile-time
Channel
C++Weekly
Topics
std::format("{}"); does not compiles while std::format("{}",1); does.
How is it possible?
(a look into std::format_string implementation, and especially its constructor? How function arguments seem to be usable in constant evaluated context?)
Length
Honestly don't know how much needs to be told...
The format string argument is a variadic template type that receives the types of the other passed parameters, and its constructor is consteval so it must run at compile time. From there it's usual constexpr/template metaprogramming to check the provided string against the given template arguments. The actual values of the other arguments can't be checked until runtime. There's a lot of different directions the video could take from there, it's rather open-ended.