Skip to content
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

Enable named arguments check in compile-time checks #2649

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,11 @@ template <typename... Args> constexpr auto count_named_args() -> size_t {
return count<is_named_arg<Args>::value...>();
}

template <typename... Args>
constexpr auto count_statically_named_args() -> size_t {
return count<is_statically_named_arg<Args>::value...>();
}

enum class type {
none_type,
// Integer types should go first,
Expand Down Expand Up @@ -3043,7 +3048,8 @@ template <typename Char, typename... Args> class basic_format_string {
std::is_reference<Args>::value)...>() == 0,
"passing views as lvalues is disallowed");
#ifdef FMT_HAS_CONSTEVAL
if constexpr (detail::count_named_args<Args...>() == 0) {
if constexpr (detail::count_named_args<Args...>() ==
detail::count_statically_named_args<Args...>()) {
Comment on lines +3051 to +3052
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this effectively make fmt::arg unusable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, fmt::arg is handled the same way as before. Consider the following table with results of boolean expressions inside this if:

master PR
w/o named arguments true true
fmt::arg false false
_a false true
fmt::arg + _a false false

This way, nothing should change for fmt::arg. In contrast with format string with a mix of named and non-named arguments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

using checker = detail::format_string_checker<Char, detail::error_handler,
remove_cvref_t<Args>...>;
detail::parse_format_string<true>(str_, checker(s, {}));
Expand Down