简体   繁体   中英

What's the difference between an explicit call and an implicit call of the conversion function?

Consider this example:

struct A{
    template<class T>
    operator T();  // #1
};
struct B:A{
    template<class U>
    operator U&&();  // #2
};
int main(){
  B  b;
  int a = b;  // #3
  b.operator int();  // #4
}

According to [class.member.lookup] p7

If N is a non-dependent conversion-function-id, conversion function templates that are members of T are considered. For each such template F, the lookup set S(t,T) is constructed, considering a function template declaration to have the name t only if it corresponds to a declaration of F ([basic.scope.scope]). The members of the declaration set of each such lookup set, which shall not be an invalid set, are included in the result.

#1 and #2 are both included in the lookup result regardless of what the conversion-function-id s are in #3 and #4 . The diagnosis for #3 is what we expect, in other words, both #1 and #2 are candidates and they are indistinguishable.

However, it seems that implementations only consider #2 as the unique candidate when processing #4 . As said above, the candidate set should be the same for either #3 or #4 . Do I omit some other rules that cause the difference? Or, Is it a bug in implementations?

Implementations just haven't caught up to the new(ly clarified) rules, which had to be largely invented in 2020 since no published standard version has ever described lookup for conversion function templates in any sensible way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM