What exactly is a namespace and why is it necessary

后端 未结 3 1205
予麋鹿
予麋鹿 2020-12-16 02:39

I am learning C++ right now, and at the beginning of every project my instructor puts a line that says:

using namespace std;

I understand t

相关标签:
3条回答
  • 2020-12-16 02:40

    From cppreference.com:

    Namespaces provide a method for preventing name conflicts in large projects.

    Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes.

    Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.

    A namespace works to avoid names conflicts, for example the standard library defines sort() but that is a really good name for a sorting function, thanks to namespaces you can define your own sort() because it won't be in the same namespace as the standard one.

    The using directive tells the compiler to use that namespace in the current scope so you can do

    int f(){
        std::cout << "out!" << std::endl;
    }
    

    or:

    int f(){
        using namespace std;
        cout << "out!" << endl;
    }
    

    it's handy when you're using a lot of things from another namespace.

    source: Namespaces - cppreference.com

    0 讨论(0)
  • 2020-12-16 02:52

    But what exactly does the line tell C++ to do. What is a namespace and what is std?

    std is a namespace.

    A namespace is a collection of symbols.

    The line tells your C++ compiler to

    a) search the included parts of namespace std (i.e. iostream, iomanip) if

    b) it does not find a definition of a symbol (such as (std::) cout ) before

    c) declaring the symbol unknown.

    Naming conflict resolution (as the other answer's describe) is probably the most important issue addressed.

    What I like most about namespaces is that the namespace contents (classes and functions) can be added across multiple header files. (In contrast to a class, which has to be completely in one header file).

    So, iostream, iomanip, string, stringstream each independently contribute classes and code to the std namespace.

    And thus I can keep most of my tools in separate files, yet merge them 'logically' into one namespace.

    For example: I have a ram based fixed size log in file dtb_log.hh, and a brief outline looks like:

    // Name:        dtb_log.hh
    //
    #ifndef DTB_LOG_HH
    #define DTB_LOG_HH
    
    namespace DTB  // <acronym description>
    {    
       class Log
       {
       public:
           // ...
    
       }; // class Log
    
    } // namespace DTB 
    
    #endif // DTB_LOG_HH
    

    I also have a (seldom used and in this case related) utility in file dtb_mem_tag.hh

    // Name:         dtb_mem_tag.hh
    //
    #ifndef DTB_MEM_TAG_HH
    #define DTB_MEM_TAG_HH
    
    
    namespace DTB    // <acronym name def>
    {
       class MemTag
       {
       public:
       // ...
       };  // class MemTag
    
    } // namespace DTB    
    
    #endif // DTB_MEM_TAG_HH
    

    Using both DTB::MemTag and DTB::Log is as simple as including the respective #include header files.

    And in the application where these are used, each usage of either can be clearly marked with the 'DTB::' prefix.


    Any DTB code 'evolution' needing work is in my 'dtb' namespace directory.

    Thus, by file date stamps alone I can tell where the transition to ubuntu 64 has had impacts to my tools. For instance, I can see that dtb_termselect.xx (for arduino interaction code) has been updated, as has dtb_popen.xx, dtb_pause, and several others.


    In a team environment (a while back), we discussed various ways to share namespaces. Generally, this was not well received ... too difficult to agree how.

    But among the ideas discussed, was the idea of using the namespace as a contributing author's signature. Thus was created DTB (mine), and KTS (Ken's Transition Service), and a few others. Not everyone was enthused about this, either.

    0 讨论(0)
  • 2020-12-16 03:00

    Let's start with a problem to explain what namespaces are. We all know that we can't have functions, classes or any other kind of data that have the same name. Let's say that we have two libraries that both add a function, let's say print(). They both give a different function, but in naming they are indistinguishable. That's where namespaces come in. A namespace is like adding a new group name to which you can add functions and other data, so that it will become distinguishable.

    namespace bla
    {
      void print()
      {
        // function bla::print()
      }
    }
    
    namespace blabla
    {
      void print()
      {
        // function blabla::print()
      }
    }
    

    Now, as you can see, there are two functions with the name print, yet there is no naming conflict, because of namespaces. The first function would be called by using: bla::print() and the second with blabla::print().

    std is an abbreviation of standard. std is the standard namespace. cout, cin and a lot of other things are defined in it. (This means that one way to call them is by using std::cout and std::cin.)

    The keyword using technically means, use this whenever you can. This refers, in this case, to the std namespace. So whenever the computer comes across cout, cin, endl or anything of that matter, it will read it as std::cout, std::cin or std::endl.

    When you don't use the std namespace, the computer will try to call cout or cin as if it weren't defined in a namespace (as most functions in your codes). Since it doesn't exist there, the computer tries to call something that doesn't exist! Hence, an error occurs.

    0 讨论(0)
提交回复
热议问题