On Tuesday, 5 June 2012 at 23:48:48 UTC, bearophile wrote:
So we are back to an idea Walter has tried and refused few
years ago, of compile-time known arguments with "static". The
small difference here is (I think) that both iPow templates are
allowed to exist in the code at the same time, and the iPow
overload with "static" is preferred by the argument is known at
compile-time:
I was also thinking about this idea today. I was writing a small
math function (Van der Corput sequence generator) with the
signature vdc(int n, int b) and noticed that the code could be
faster when b == 2 (the most common case) because divides can be
turned into shifts and mods turned to bitwise AND etc.
You could duplicate the function to take the static args as
template args, but that's ugly.
I also came to the conclusion of using 'static' as a parameter
"storage class" to specify that the parameter is known at
compile-time.
One problem with this approach is that it only solves some cases
and cannot work in general. It also has other implications:
- Code with 1 or more optimised versions will require extra
maintenance/testing.
- Makes code more difficult to reason about (can be difficult to
tell which is version is called).
- Adds more rules for overload resolution.
However, the biggest problem with this proposal (in my opinion)
is that it is unnecessary. I care deeply about performance, but
tiny optimisations like this are simply not important 99% of the
time. When they are important, just write a specific optimised
version and use that. Yes, you lose generality, but special needs
call for special cases. Let's not complicate the language and
bloat the codebase further for questionable gain.