Since you can't use contexpr
... Regarding your questions:
Unless you are searching for the next biggest prime number, you shouldn't be bothered that simple calculations take place once at startup. Try to measure it and it is likely that you discover that the initialisation is done in less than a milisecond.
That said, values computed at startup behave as variables (have to be
asked
for their value everytime they are used), whereas compile-time constant's value is always known. Hence the former might be a bit slower, but likely without any significance.Always measure first before introducing an inconvenience.
Again, why do you care? If
Filter
type for particularx
is not used anywhere in the code, why should the value be around there somewhere?Template statics are problematic if they depend on each other - in your case they don't, each
f
is autonomonous.
Having said all that, a great tool to tinker around is http://gcc.godbolt.org/ - you see the assembly as you type. It does NOT support MS compilers, but it gives you a pretty good guess about how compilers optimise stuff away.
If your disrtibution is simple enough to be a macro it will be a compile-time constant:
#define someDistribution(x) x * x
template <int x> struct Filter
{
static const float f;
};
template <int x> const float Filter<x>::f = someDistribution(x);
int main()
{
return Filter<200>::f + Filter<100>::f;
}
The assembly (Clang):
main: # @main
movl $50000, %eax # imm = 0xC350
ret
If you change someDistribution
to be a function, even inline one, you'll see that computation will have to take place.
EDIT: remember, that you can do maaany thing with macros, including "specialising" them for certain values. Simple distribution should be preprocessor-friendly.