jingweimo的个人博客分享 http://blog.sciencenet.cn/u/jingweimo

博文

[转载]Why is “using namespace std” considered bad practice

已有 1647 次阅读 2019-1-10 12:31 |系统分类:科研笔记|文章来源:转载

 Question from Stack Overflow (https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice):

I've been told by others that writing using namespace std in code is wrong, and that I should use std::cout and std::cin directly instead.

Why is using namespace std considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Does it impact performance?


Answer by Greg Hewgill

This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar:

using namespace foo;using namespace bar;

Everything works fine, you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

If you had used foo::Blah() and bar::Quux(), then the introduction of foo::Quux() would have been a non-event.




https://blog.sciencenet.cn/blog-578676-1156311.html

上一篇:[转载]Build OpenCV for CUDA support
下一篇:[转载]Color spaces in OpenCV
收藏 IP: 98.209.56.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-30 02:41

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部