universal reference vs const reference priority?

前端 未结 1 2074
长发绾君心
长发绾君心 2021-02-07 11:40

When I consider the two following overloads:

template  void f(const T&... x);
template  void f(const T& x);
相关标签:
1条回答
  • 2021-02-07 12:34

    There is not a universal priority between these two functions. They compete equally in the overload resolution algorithm. In general the so-called "universal reference" wins unless const T& is an exact match, and there the const T& wins.

    struct A {};
    
    int
    main()
    {
        f(std::declval<A>());  // calls f<A>(A&&), #1
        f(std::declval<const A>());  // calls f<const A>(const A&&), #1
        f(std::declval<A&>());  // calls f<A&>(A&), #1
        f(std::declval<A&&>());  // calls f<A>(A&&), #1
        f(std::declval<const A&&>());  // calls f<const A>(const A&&), #1
        f(std::declval<const A&>());  // calls f<A>(const A&), #2
    }
    

    Good advice is to never overload like this.

    0 讨论(0)
提交回复
热议问题