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

后端 未结 5 600
忘掉有多难
忘掉有多难 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: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"
    }
    

提交回复
热议问题