Using std Namespace

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

    using namespace std imports the content of the std namespace in the current one. Thus, the advantage is that you won't have to type std:: in front of all functions of that namespace. However, it may happen that you have different namespaces that have functions of the same name. Thus, you may end not calling the one you want.

    Specifying manually which ones you want to import in std prevents that from happening, but may result in a long list of using at the beginning of your file, which some developer will find ugly ;) !

    Personally, I prefer specifying the namespace each time I use use a function, except when the namespace is too long, in which case I put some using at the beginning of the file.

    EDIT: as noted in another answer, you should never put a using namespace in a header file, as it will propagage to all files including this header and thus may produce unwanted behavior.

    EDIT2: corrected my answer, thanks to Charles comment.

提交回复
热议问题