Home > Mobile >  Are constraints in overload resolution affected by difference it type qualifiers?
Are constraints in overload resolution affected by difference it type qualifiers?

Time:05-17

Having the following simple code:

#include <concepts>

auto f(const auto&) { }
auto f(std::integral auto) {}

int main() 
{ 
    f(5); 
}

We have an ambiguous call with clang & gcc but MSVC chooses the more constrained one. So far I found nothing that would support the clang's & gcc's behavior. So is it a bug in both compilers or there is something that makes this call ambiguous?

CodePudding user response:

Without considering constraints, the call is ambiguous because the first overload is deduced to a function parameter type const int& and the second to int. Neither will be considered better than the other when called with a prvalue of type int and neither const auto& or auto are more specialized in usual partial ordering of templates either.

According to [temp.func.order]/6.2.2 constraints on function templates are not taken into account if the types of the function parameters after substitution from template argument deduction do not correspond.

Here the first overload deduced the function parameter to const int& and the second to int. These are not equal. And so partial ordering of the templates will not consider either more specialized than the other based on any constraints either.

  • Related