0

Here is an example of two namespaces in two different files:

There is a namespace definition in file1.cpp where the main function is located like this. This namespace is defined outside of main(), so it is global.

namespace {
  char const * const GID{ "SOMENAME" };
  int summary( void );
}

Then in file2.cpp, the same GID is defined as:

namespace {
  char const * const GID{ "SOMEOTHER_NAME" };
}

I think the GID constant char only sits in corresponding file. For example, SOMEOTHER_NAME is only used in file2.cpp. But if that's the purpose, isn't it better just use a different name than GID?

Also, if I want to use the summary() function in file1.cpp, then how to use the namespace in file1? Note that the namespaces don't have names.

visitor99999
  • 163
  • 1
  • 10
  • This answer might help - https://stackoverflow.com/a/9224641/15310387 - use a header file to define something you want usable in multiple files. – Mr R May 23 '21 at 00:52
  • References: [Unnamed/anonymous namespaces vs. static functions](https://stackoverflow.com/questions/154469) and [Anonymous Namespace Ambiguity](https://stackoverflow.com/questions/3673353) – JaMiT May 23 '21 at 01:21
  • 1
    *This namespace is defined outside of main(), so it is global.* This is a good thing. Unless things changed very recently, you can't define a namespace inside of a function. – user4581301 May 23 '21 at 01:33
  • This c++ reference link: https://en.cppreference.com/w/cpp/language/namespace indicates namespaces can be nested so one way to use summary() is turn anonymous namespaces into a named higher level namespace and put summary() inside. Then others can see summary(). – visitor99999 May 23 '21 at 02:58

1 Answers1

3

Anonymous namespaces, such as you have declared, are inaccessible other than in the file they are declared or in files in which the declaration is included. In the file it is declared, the namespace is accessible without using a specific namespace. So in file1, summary() is accessed as summary. The GID declarations in file1 and file2 are in two different namespaces, so do not conflict with each other.

In effect, each anonymous namespace has a unique namespace name, but you are unable to access the name, which is why you cannot access an anonymous namespace from a file that does not contain or include that specific anonymous namespace.

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
  • but how to use summary() in file2.cpp? Or you mean it's not accessible from file2.cpp at all? – visitor99999 May 23 '21 at 00:55
  • @visitor99999 _"it's not accessible from file2.cpp at all?"_ Exactly that. It's finally the purpose of anonymous namespaces. Otherwise that could be tricked by the linker. – πάντα ῥεῖ May 23 '21 at 00:59
  • ok, thanks. It's better move summary() out of the namespace in file1.cpp then since it should be accessible from file2.cpp as well. – visitor99999 May 23 '21 at 01:08
  • @visitor99999 Yes. The purpose of anonymous namespaces is to restrict accessibility. – rsjaffe May 23 '21 at 01:14