Using std Namespace

前端 未结 16 971
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:57

There seem to be different views on using \'using\' with respect to the std namespace.

Some say use \' using namespace std\', other say don\'t but rathe

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 05:32

    Excluding the basics (Having to add std:: infront of all stl objects/functions and less chance of conflict if you don't have 'using namespace std')

    It is also worth noting that you should never put

    using namespace std
    

    In a header file, as it can propagate to all files that include that header file, even if they don't want to use that namespace.

    In some cases it is very beneficial to use things like

    using std::swap
    

    As if there is a specialized version of swap, the compiler will use that, otherwise it will fall back on std::swap.

    If you call std::swap, you always use the basic version, which will not call the optimized version (if it exists).

提交回复
热议问题