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

Compile-time error for incorrect usage. #1

Open
Victor-Savu opened this issue Oct 26, 2014 · 1 comment
Open

Compile-time error for incorrect usage. #1

Victor-Savu opened this issue Oct 26, 2014 · 1 comment
Labels

Comments

@Victor-Savu
Copy link
Owner

At the moment, the formatting crashes when the format string contains more placeholders than the number of arguments passed to the formatting operator():

std::cout << "Hello %!\n"_fmt();

However, it is perfectly possible to test at compile time if a given list of parameters would cause problems:

constexpr auto f = "Hello, %! How is % feeling today?"_fmt;

constexpr auto cnt_1 = f.check("Paul", "Chani");
static_assert(cnt_1.first == cnt.second,
             "Wrong number of arguments."); // Ok!

constexpr auto cnt_2 = f.check("Paul");
static_assert(cnt_2.first == cnt_2.second,
             "Wrong number of arguments."); // Compile-time ERROR!

The check() method returns a pair of numbers, one representing the number of placeholders '%' in the format string and another representing the number of parameters passed. Both are computed in a constexpr context.

Unfortunately, the static_assert cannot be performed inside the formatting operator() of the format string due to the c++14 rules for constexpr. Within the member operator, the object cannot be aware of its own "constexpr"-ness, so it is unable to compute the number of placeholders '%' in a constexpr context.

@Victor-Savu
Copy link
Owner Author

maybe c++17's if constexpr could help? Need to investigat further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant