Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"using function-name" can only hide ordinary function?

Tags:

c++

It seems that "using function-name" can only hide the ordinary function, but cannot hide the friend functions of the operand or the functions in the same namespace of the operand. Am I understanding this correctly?

example 1:

void swap(int)
{

}

void foo()
{
    using std::swap;
    int i=10,j=20;
    swap(i);  //compile error ,because  std::swap hidden void swap(int)
}

example 2:

class Cat {
    friend void swap(Cat&, Cat&);
};
void swap(Cat &lhs, Cat &rhs)
{
    cout<<"call cat friend swap"<<endl;
}

class Foo
{
    public:
        Cat h;
};

void swap(Foo &lhs, Foo &rhs)
{
    using std::swap;
    swap(lhs.h, rhs.h); //compile ok. will print out
                        //call cat friend swap
}
like image 660
camino Avatar asked Sep 14 '26 01:09

camino


1 Answers

Your observation is correct.

using introduces a name at the local level, which hides names in enclosing namespaces. But name search also uses argument-dependent lookup, which will still work (and might find some of the hidden names, too). If you want to find std::swap and nothing else, then write std::swap(i);.

like image 158
Ben Voigt Avatar answered Sep 15 '26 15:09

Ben Voigt