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

后端 未结 5 576
忘掉有多难
忘掉有多难 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 18:59

    No, but you can tell your coworkers that you should never have a using directive or declaration in a header.

    0 讨论(0)
  • 2021-01-03 19:04

    No, C++ Standard doesn't say anything about "undo". The best you are allowed to do is to limit scope of using:

    #include <vector>
    
    namespace Ximpl {
    
    using namespace std;    
    vector<int> x;
    
    }
    
    vector<int> z; // error. should be std::vector<int>
    

    But unfortunately using namespace Ximpl will bring all names from std namespace as well.

    0 讨论(0)
  • 2021-01-03 19:06

    Not to my knowledge... But as a rule I only use "using namespace" in .cpp files.

    0 讨论(0)
  • 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<string,complicated_stuff*> 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

    0 讨论(0)
  • 2021-01-03 19:10

    As others said, you can't and the problem shouldn't be there in the first place.
    The next-best thing you can do is bring in your needed symbols so that they are preferred by the name look-up:

    namespace A { class C {}; }
    namespace B { class C {}; }
    using namespace A;
    using namespace B;
    
    namespace D {
        using A::C; // fixes ambiguity
        C c;
    }
    

    In some cases you can also wrap the offending includes with a namespace:

    namespace offender {
    #  include "offender.h"
    }
    
    0 讨论(0)
提交回复
热议问题