Anonymous Namespace Ambiguity

前端 未结 5 2027
我在风中等你
我在风中等你 2020-12-18 19:51

Consider the following snippet:

void Foo() // 1
{
}

namespace
{
  void Foo() // 2
  {
  }
}

int main()
{
  Foo(); // Ambiguous.
  ::Foo(); // Calls the Foo         


        
5条回答
  •  时光说笑
    2020-12-18 20:16

    While Georg gives standard-complient, correct, right, and respectable answer, I'd like to offer my hacky one - use another namespace within the anonymous namespace:

    #include 
    
    using namespace std;
    
    namespace
    {
    namespace inner
    {
        int cout = 42;
    }
    }
    
    int main()
    {
        cout << inner::cout << endl;
        return 0;
    }
    

提交回复
热议问题