If you overload a function and then call it with an argument that perfectly matches one of the overloads
int f(int){return 3;}
int f(bool){return 4;}
... //inside main()
f(1); //Calls f(int)
the compiler simply chooses this (perfect) match before attempting any implicit conversions. However I’ve been trying to overload a function tempĺate as in
template <bool veracity>
int f(){return 1;}
template <int amount>
int f(){return 2;}
... //inside main()
f<1>();
but the compiler keeps complainning about ambiguous call to the overloaded f(), stating it could be either f<true>() or f<1>(). Shouldn’t the compiler just choose the perfect match, instead of trying to convert 1 to true ?
I was under the impression that implicit conversion for template arguments was actually more restrictive than implicit conversion of function arguments. Is there a way to get around this problem?
The argument you’re supplying isn’t a type, it’s a value, so the rules are a bit different — you need to apply the rules for non-type arguments. For non-type arguments, implicit conversions are allowed. §14.3.2/5:
In C++03, the wording is marginally different, but the effect essentially identical (also §14.3.2/5):
Either way, since
1is both anintand implicitly convertible to abool, your call is ambiguous.