简体   繁体   中英

How do parentheses around the function name change which function is called?

I could not understand the second foo call in the code below. How does it call global foo function. Why does (foo) call struct A's int()? Can you help me?

#include <stdio.h>
#include <utility>
#include <iostream>
using namespace std;

namespace MySpace{
    struct A{
        operator int () const {
            cout <<"operator" << endl;
           return 1;        
        }
    };
    
    void foo(A){
        std::cout<< "1" << endl;
    }
}

void foo(int){
    std::cout << "--2" << endl;
}


int main()
{
    
    MySpace::A x;
    foo(x);
    (foo)(x);
   
    return 0;
}

I could not understand the second foo call. How does it call global foo function. Why does (foo) call struct A's int()? Can you help me?

The 1st one works because ADL finds MySpace::foo and it wins in overload resolution against ::foo and gets called.

For the 2nd one, adding parentheses like (foo) prevents ADL; then MySpace::foo can't be found, only ::foo is found and gets called. A is converted to int implicitly (by A 's conversion operator) for it to be called.

BTW: You can mark the conversion operator as explicit to forbid the implicit conversion from A to int . Then the 2nd one would fail. Eg

namespace MySpace {
    struct A{
         explicit operator int () const {
            cout <<"operator" << endl;
           return 1;        
        }
    };
    
    void foo(A){
        std::cout<< "1" << endl;
    }
}

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