We are familiar with overloading based on function parameters. But why can't we have overloading based on non-type template parameters? With such overloading, you don't have to add extra function parameters just for overloading purposes, which may have a negative impact on runtime performance. Alas, the following code does not compile:
template <bool>
void func() {}
template <int>
void func() {}
int main() {
func<0>();
}
The error message produced is
error: call of overloaded 'func()' is ambiguous
func<0>();
^
note: candidate: void func() [with bool <anonymous> = false]
void func() {}
^
note: candidate: void func() [with int <anonymous> = 0]
void func() {}
^
Note that this may be more efficient than
void func(bool) {}
void func(int) {}
Is there any problem in allowing this usage?
int
with, say,std::ostream&
, then it works just fine. Might be VS-specific, though.bool
one, because there's no mini-overload-resolution for template arguments; everything that's a valid converted constant expression of the template parameter's type is equally good. Regardless, if you actually see a negative impact from dummy parameters passed to control overload resolution, you should be filing bugs with your compiler vendor, not contorting your code (further).func(bool)
vsfunc<true>()
makes a difference in the performance your application, I would say you are in pretty good shape.