Why is namespace composition so rarely used?

前端 未结 4 1734
不思量自难忘°
不思量自难忘° 2021-02-18 23:57

In his book The C++ Programming Language (third edition) Stroustrup teaches to define individual components in their own namespace and import them in a general namespac

4条回答
  •  时光取名叫无心
    2021-02-19 00:25

    I feel the other answers have left out the first, simplest, and most useful way to compose namespaces: to use only what you need.

    namespace Needed {                                                                                                                                                                            
      using std::string;                                                                                                                                                                          
      using std::bind;                                                                                                                                                                            
      using std::function;                                                                                                                                                                        
      using std::cout;                                                                                                                                                                            
      using std::endl;                                                                                                                                                                            
      using namespace std::placeholders;                                                                                                                                                          
    }                                                                                                                                                                                             
    
    
    int main(int argc, char* argv[])                                                                                                                                                              
    {                                                                                                                                                                                             
    
      /*  using namespace std;                                                                                                                                                                    
          would avoid all these individual using clauses,                                                                                                                     
          but this way only these are included in the global                                                                                                                                      
          namespace.                                                                                                                                                                          
      */                                                                                                                                                                                          
    
     using namespace Needed;  // pulls in the composition
    
     string s("Now I have the namespace(s) I need,");
    
     string t("But not the ones I don't.");
    
     cout << s << "\n" << t << endl;                                                                                               
    
     // ...
    

    Thus any conflicts with other parts of the STL are avoided,as well as the need to preface the common used functions with std::.

提交回复
热议问题