Tagged: ,

Viewing 0 reply threads
  • Author
    Posts
    • #9538
      RainerRainer
      Keymaster
        Up
        0
        Down
        ::
        namespace {
            int cannotAccessOutsideThisFile() { ... }
            void cannotCallOutsideThisFile(){ ... }
        } // namespace 
        

        An anonymous namespace ensures that its content cannot be accessed from outside the file. All identifiers used in an anonymous namespace have internal linkage. Anonymous namespaces are typically used when you have a lot of content that you want to ensure stays local to a given file. You can have the same effect for a function, when you declare a function as static.


        static void cannotCallOutsideThisFile(){ ... }

        The main reason for anonymous namespaces it to get rid of this usage of the keyword static (https://github.com/isocpp/CppCoreGuidelines/issues/1727). Read more in the C++ Core Guidelines: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-unnamed2

         

    Viewing 0 reply threads
    • You must be logged in to reply to this topic.