I'm writing a special print function that produces a cstdio printf - statement at compile time. The idea is basically that you invoke a function special_print() with a variadic parameter list and the function will assemble the necessary printf - statement at compile time. Here's what I've got:
#include <cstdio>
#include <string_view>
// how to implement "expand" from below?
template <typename... Ts>
constexpr const char* cxpr_format = /* ... this one I've already figured out */
template <typename... Ts>
void special_print(Ts... arg)
{
printf(cxpr_format<Ts...>, expand(arg)...);
// should expand to printf("%.*s%d", string_view.size(), string_view.data(), int) with below function call
}
int main()
{
std::string_view strview = "hello world";
int integer = 2;
special_print(strview, integer);
}
I've already figured out the part for creating my format string - that one is out of the question. But now comes the problem: In case when I've got a string_view passed in as an argument, I would need to conditionally expand it to two arguments to printf: one for the initial char pointer and one for the size which goes together with the %.*s format specifier. And this is the real crux. Can this be done somehow in C++ (at compile time)?
Note: Before any further questions arise I would like to point out that I have no idea on how to do this in C++ and this is my question. Every approach will be gladly taken into account!