2
namespace A {
  void F() {}
  namespace B {
    void F(int) {}  
  }
}

using A::B::F;

namespace A {
  void G() {
    F();   // OK
    F(1);  // Error: too many arguments to function void A::F()
  }
}

int main() { return 0; }

I have this piece of code.

I defined two functions with same names but different signatures.

Then I use a using-declaration using A::B::F.

In A::G() compiler tries to resolve A::F() before A::B::F().

Are there any orders if there are such conflicts?

2 Answers 2

4

The deepest nested scope is searched first, and scopes are then searched outward if the name is not found. So first it would find a block-scope declaration of F inside G, if any; then it would find a declaration at the namespace scope of A, if any; and if that too failed it would search the global scope. Since using A::B::F; appears at global scope, A::F is always found first. Perhaps you should move the using declaration inside A.

Sign up to request clarification or add additional context in comments.

Comments

0

It's definitely about placement.

namespace A {
  void G() {


    F();   // OK, A::F

    using B::F;
    F(1);  // OK, A::B::F
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.