Can someone please explain to me why in the following code I get an ambiguous call to the overloaded constructors in Foo if I attempt to instantiate MyFooC? My assumption was that the integer used as a constructor parameter would have been promoted to an unsigned int and been resolved but this is obviously incorrect.
template <typename t> class Foo
{
private:
t m_Value;
unsigned int m_Length;
public:
Foo(const t& Value) :
m_Value(Value),
m_Length(0)
{
}
Foo(unsigned int Length) :
m_Value(static_cast<t>(0)),
m_Length(Length)
{
}
};
int main()
{
Foo<double> MyFooA(32U);
Foo<double> MyFooB(32.0f);
//Foo<double> MyFooC(32);
return 0;
}
An int
can be converted to both a double
and an unsigned
; both are
considered "widening" conversions, and have equal rank. As a general
rule, any time you overload, and one of the overloads is an integral
type, it's a good idea to overload on int
as well, to be sure of
getting what you want, and avoiding ambiguities, when someone tries to
pass an integral constant.
BTW: although it is clear in context what you mean, in the C++ standard,
"promotion" has a very precise meaning, and does not include a
conversion of int
to unsigned int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With