Partial ordering of forwarding reference and normal reference with deduction guides

Viewed 189

gcc 8.0.0 and clang 5.0.0 disagree on the behavior of this program:

#include <iostream>

template <typename T>
struct A {
    A(const T&) { std::cout << __PRETTY_FUNCTION__ << '\n'; }
    A(T&&)      { std::cout << __PRETTY_FUNCTION__ << '\n'; }
};

template <typename U> A(U&&) -> A<double>;

int main() {
    int i = 0;
    const int ci = 0;

    A a1(0);  // both say A<double>
    A a2(i);  // both say A<double>
    A a3(ci); // gcc says A<int>, clang says A<double>
}

gcc's behavior doesn't make sense to me - if the const T& overload is preferred to the U&& overload for lvalue const int, why isn't the T&& overload preferred to the U&& overload for rvalue int? clang's makes more sense to me (none of the functions is more specialized than the other, so deduction guide wins).

Who's right?

1 Answers
Related