Using std Namespace

前端 未结 16 695
佛祖请我去吃肉
佛祖请我去吃肉 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:31

    Both

    using std::string;
    

    and

    using namespace std;
    

    add some symbols (one or lots of) to the global namespace. And adding symbols to global namespace is something you should never do in header files. You have no control who will include your header, there are lots of headers that include other headers (and headers that include headers that include headers and so on...).

    In implementation (.cpp) files it's up to you (only remember to do it after all #include directives). You can break only code in this specific file, so it's easier to manage and find out the reason of name conflict. If you prefer to use std:: (or any other prefix, there can be many namespaces in your project) before indentifiers, it's OK. If you like to add identifiers you use to global namespace, it's OK. If you want to bring whole namespace on your head :-), it's up to you. While the effects are limited to single compilation unit, it's acceptable.

提交回复
热议问题