Can I undo the effect of “using namespace” in C++?

后端 未结 5 615
忘掉有多难
忘掉有多难 2021-01-03 18:16

With using namespace I make the whole contents of that namespace directly visible without using the namespace qualifier. This can cause problems if using

5条回答
  •  臣服心动
    2021-01-03 19:08

    The closest, that I'll try to use in header files is following:

    //example.h
    
    #ifndef EXAMPLE_H_
    #define EXAMPLE_H_
    
    
    /**
     * hating c++ for not having "undo" of using namespace xx
     */
    #define string std::string
    #define map std::map
    
    class Example {
    public:
        Example (const char *filename);
        Example (string filename);
        ~Example ();
    private:
        map my_complicated_map;
    
    };
    
    #undef string
    #undef map
    
    #endif //EXAMPLE_H_
    

    after all, defines are #undef -able. There are 2 problems: 1. it is ugly 2. separate #define and #undef for each name from the corresponding namespace are used

提交回复
热议问题