What's the scope of the “using” declaration in C++?

后端 未结 7 652
醉话见心
醉话见心 2020-12-07 19:45

I\'m using the \'using\' declaration in C++ to add std::string and std::vector to the local namespace (to save typing unnecessary \'std::\'s).

using std::str         


        
7条回答
  •  心在旅途
    2020-12-07 20:03

    The scope of the using statement depends on where it is located in the code:

    • Placed at the top of a file, it has scope throughout that file.
    • If this is a header file, it will have scope in all files that include that header. In general, this is "not a good idea" as it can have unexpected side effects
    • Otherwise the using statement has scope within the block that contains it from the point it occurs to the end of the block. If it is placed within a method, it will have scope within that method. If it is placed within a class definition it will have scope within that class.

提交回复
热议问题