03.06.2015 23:45, Abel Sinkovics:
This approach to write a type-safe printf displays the characters one-by-one on an output stream. The printf library in Mpllibs does the validation at compile-time and calls the "unsafe" printf at runtime (and has therefore no runtime overhead). Well that's assuming that printf() is efficient at runtime. I would like to hope that
string s = "hello" + t + "world";
would be more efficient than
string s = wrapper_around_printf_returning_string("hello%sworld",t.c_str());
If it isn't, we're in trouble :-) Given that the format string is known at compile-time, it should be possible to generate "optimal" code for it using a metaprogram. But that is significantly more complicated than what safe_printf currently offers.
JFYI, I made small test some time ago with "formatting" stuff at compile-time (ad-hoc parsing, without Metaparse). https://github.com/panaseleus/ctte Following code: { int counter = 2; char character = '!'; double value = 0.5; for_each_part ( auto x, "val = $value$, cnt = $counter$, ch = $character$, again v=$value$;\n", counter, character, value ) { print_it(x); }; } Produces identical ASM code to handwritten version: { int counter = 2; char character = '!'; double value = 0.5; print_it("val = "); print_it(value); print_it(", cnt = "); print_it(counter); print_it(", ch = "); print_it(character); print_it(", again v="); print_it(value); print_it(";\n"); } Best Regards, Evgeny Panasyuk