jeudi 30 juillet 2015

Why doesn't narrowing affect overload resolution?

Consider the following:

struct A {
    A(float ) { }
    A(int ) { }
};

int main() {
    A{1.1}; // error: ambiguous
}

This fails to compile with an error about an ambiguous overload of A::A. Both candidates are considered viable, because the requirement is simply:

Second, for F to be a viable function, there shall exist for each argument an implicit conversion sequence (13.3.3.1) that converts that argument to the corresponding parameter of F.

While there is an implicit conversion sequence from double to int, the A(int ) overload isn't actually viable (in the canonical, non-C++-standard sense) - that would involve a narrowing conversion and thus be ill-formed.

Why are narrowing conversions not considered in the process of determining viable candidates? Are there any other situations where an overload is considered ambiguous despite only one candidate being viable?

Aucun commentaire:

Enregistrer un commentaire