Suppose I have a function that takes a string as input:
SomeOutputType f_impl(const char* s);
Most call sites just use string literals as input, e.g. f("Hello, world")
. Suppose I have implemented the following function to compute the result at compile time
template <char...> SomeOutputType f_impl();
My question is, is there a way to let the call sites like f("Hello, world")
calls the templated form, while for general call sites like string s="Hello, world"; f(s.c_str());
calls the general form? For clarification, auto s = "Hello, world"; f(s);
don't have to call the templated form because s
is now a variable and no longer a compile time constant.
A useful case for this question is to optimize printf
. In most cases the format
will be string literals so a lot of things can be done at compile time to optimize things, instead of parsing the format
at runtime.
f
and it somehow dispatches to different forms off_impl
according to whether the call site uses string literal or not.