Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 930
Veriginia Avatar asked Dec 05 '25 01:12

Veriginia


1 Answers

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. E.g.

namespace MySpace {
    struct A{
         explicit operator int () const {
            cout <<"operator" << endl;
           return 1;        
        }
    };
    
    void foo(A){
        std::cout<< "1" << endl;
    }
}
like image 159
songyuanyao Avatar answered Dec 06 '25 15:12

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!