What I would like to do is to create:
template<Args... args)>
int println(Args...) {
// implementation which calls:
// printf("<string literal format string at compile time>", args...);
// additional perk would be compile time type checking
// I expect to provide a format string for each type by some template
// specialization.
}
I have been analyzing two interesting pieces of work with compile time string literals:
Compile time memory aligned string literals
100% constexpr string implementation
http://sourceforge.net/p/constexprstr/code/HEAD/tree/no-pp-constexpr_string.cpp
Basically I am more or less able to statically infer length of required string literal for format but nothing more as compiler refuses to treat my work as constexpr. Another big problem is when using constexpr strings from the above link the compiler is never able to infer the size of the resulting string.
The more I try to achieve this the more my ignorance outweighs my enthusiasm. I would appreciate any tips and/or code samples that solve some or all of the problems with doing such an implementation.
Note: I am not looking for advice regarding using different forms of logging such as through cout.
Note2: The should not use any std::string as those are runtime
Note3: Generally all the type-safe printfs are using different approaches and I am familar that this could be easily done with multiple calls to printf
. I would like to do it in a single call. I also know that the buffer could be incrementally built but this question is about constructing the format string. :)
Update: Basically what part of the code needs to achieve is
constexpr const char* formatString = build_compile_time_format_string(args...);
// build_compile_time_format_string(3, "hi", -3.4)
// should evaluate to "%d %s %f"
// or to "%d hi %f"
formatString(3, "hi", -3.4)
to evaluate to"%d %s %f"
? – Chihuahuaconsole.log("Hello", a, b, "c")
. I know it could be easily done with multiple calls toprintf
but I want only one. :) – Satanism"%s"
? (see the diff between you update and @MooingDuck comment). – Vitale