c++ - universal reference vs const reference priority? -


when consider 2 following overloads:

template <class... t> void f(const t&... x); template <class t> void f(const t& x); 

i have guarantee f(x) call second function , never lead ambiguity. in sense second version universally prioritized compared first 1 one argument whatever type is.

now consider situation there universal reference , const reference versions of function:

template <class t> void f(t&& x); template <class t> void f(const t& x); 

my question is: universal priority between these 2 functions regardless of type of x (r-value reference, reference, cv-qualifiers, pointer...) in previous case? (and if yes, priority ?)

there not universal priority between these 2 functions. compete equally in overload resolution algorithm. in general so-called "universal reference" wins unless const t& exact match, , there const t& wins.

struct {};  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 never overload this.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -