Better compiler errors for invalid ""_format usage

In particular, this uses FMT_ENABLE_IF() to constrain the function to only
valid combinations of the format string and arguments. This ensures that there
is an actual error at the usage site, which puts the red squiggles on the
incorrect code, rather than inside the library.
This commit is contained in:
Mathias Stearn 2019-05-30 13:53:58 -04:00
parent d07cc2026b
commit 997e2747ee

View File

@ -3566,14 +3566,17 @@ namespace internal {
# if FMT_USE_UDL_TEMPLATE
template <typename Char, Char... CHARS> class udl_formatter {
public:
template <typename... Args>
FMT_CONSTEXPR11 static bool is_valid() {
return do_check_format_string<Char, error_handler, Args...>(
basic_string_view<Char>(
std::initializer_list<Char>{CHARS...}.begin(),
sizeof...(CHARS)));
}
public:
template <typename... Args, FMT_ENABLE_IF(is_valid<Args...>())>
std::basic_string<Char> operator()(const Args&... args) const {
FMT_CONSTEXPR_DECL Char s[] = {CHARS..., '\0'};
FMT_CONSTEXPR_DECL bool invalid_format =
do_check_format_string<Char, error_handler, Args...>(
basic_string_view<Char>(s, sizeof...(CHARS)));
(void)invalid_format;
return format(s, args...);
}
};