1

When it comes to using namespaces it is really good thing to avoid name clashes. But the thing that matters me: Using anonymous namespaces.

Here in my example for some reason I defined two un-named namespaces and accidently the two has a member function with the same signature. And I want to define the two outside the namespaces.

namespace {
    void foo(); // to be defined outside in somewhere else
    int x = 0; // I want the above foo() to increment x
}

namespace {
    void foo();  // to be defined outside in somewhere else
                 // I want foo to do something else
}

// Here is my guess how to define a function outside Anonymous NS.
void ::foo() {
    cout << "::foo()" << endl;
}

// void foo(){
//   cout << "foo()" << endl;
//}

int main(){

    foo();
    ::foo();

    cout << endl;
    cin.get();
    return 0;
}
  • How to avoid name clashes when having multiple Un-Named-Namespaces?

Thank you.

WonFeiHong
  • 445
  • 4
  • 16
  • 1
    do not use unamed namespaces – Jake Freeman Dec 23 '17 at 17:43
  • @JakeFreeman: Yes. But only I wanted to understand. – WonFeiHong Dec 23 '17 at 17:44
  • 4
    All the members are part of the same namespace. You are just extending the same namespace by having multiple anonymous namespaces in a single translation unit. In your case you are just re-declaring a function. – Ron Dec 23 '17 at 17:45
  • 1
    WibFeilHong it is the same as having two `void foo()` functions in the global namespace – Jake Freeman Dec 23 '17 at 17:47
  • 1
    There ain't *multiple Un-Named-Namespaces*. There is only one. – Walter Dec 23 '17 at 17:55
  • 1
    You could utilize function pointers. – Jake Freeman Dec 23 '17 at 18:01
  • @JakeFreeman Is this a general tip not to use anonymous namespaces? If so why is that? Thanks. – haaawk Dec 23 '17 at 19:30
  • The point of namespaces is to group together classes, functions, etc. into a name like std or boost. When using unnamed namespaces you are not naming the items and you are basically coding in the global namespace @haaawk the answers to this question also help explain: https://stackoverflow.com/questions/357404/why-are-unnamed-namespaces-used-and-what-are-their-benefits – Jake Freeman Dec 23 '17 at 19:33
  • @JakeFreeman How would you make something local to translation unit then? I know there's 'static' but it does not work with types, does it? – haaawk Dec 23 '17 at 19:35
  • @haaawk static and extern both work with types – Jake Freeman Dec 23 '17 at 19:36
  • 1
    @JakeFreeman according to this SO question static does not work with user defined types -> https://stackoverflow.com/questions/4422507/superiority-of-unnamed-namespace-over-static "Unnamed namespace is superior to static keyword, primarily because the keyword static applies only to the variables declarations and functions, not to the user-defined types." – haaawk Dec 23 '17 at 19:41
  • @haaawk user defined types can be static just not declare static class. It is very rare were you need the functionally of a static class. Finally most of what you are quoting was removed in later additions of C++ – Jake Freeman Dec 23 '17 at 19:45
  • @JakeFreeman What do you mean by "[...]types can be static just not declare static[...]"? What do you mean by first static? with internal linkage? – haaawk Dec 23 '17 at 19:56

0 Answers0