-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
constexpr checks of format strings #546
Comments
Potentially relevant discussion about constexpr function parameters: https://groups.google.com/a/isocpp.org/d/msg/std-proposals/RdAK-0RyiY0/F3h-qF_jWbIJ |
It is possible to access the format string and argument types at compile time using UDLs: #include <string>
template <char... Chars>
struct formatter {
template <typename... Args>
constexpr std::string operator()(Args...) const;
};
template <typename T, T... Chars>
constexpr auto operator""_format() {
return formatter<Chars...>();
}
int main() {
return "{}"_format(42).size();
} |
Not only could you perform the safety checks at compile-time, but you could also generate optimal code by parsing everything at compile-time. |
Parsing of format specs is now |
@vitaut tried to compile your std branch with "g++ (SUSE Linux) 7.2.1 20171020 [gcc-7-branch revision 253932]" in order to test constexpr but got a lot of errors. Is your CI catching those? |
@Rush, the |
Add a
check_format
constexpr
function for performing a compile-time format string check and a section explaining why such check cannot be incorporated in the format function itself at the moment. As discussed in #492 and the main problem is the lack of ability to handle the format string inconstexpr
context when other arguments are notconstexpr
.Also Zach Laine suggested to mention the benefits of "compile-time strings":
The text was updated successfully, but these errors were encountered: