On namespace 'names': ::std:: vs std::

♀尐吖头ヾ 提交于 2019-12-23 17:10:30

问题


I have been looking over some posts here on Stackoverflow, and I have noticed that most people use std:: but some people uses ::std::

I think i have read something about a global scope or something like that in namespaces as a reason to use ::std:: (but i can't find it now, because it was in a comment to an unrelated question)

Is there any reason to prefer one way versus the other?


回答1:


It's a bad idea to write code like this, but you could:

namespace foo {
    namespace std {
        int bar;
    }
    std::string s;
}

In that case, the std::string refers to the ::foo::std namespace, not the ::std namespace. So, using ::std::string is just being a little bit more unambiguously careful.



来源:https://stackoverflow.com/questions/18544651/on-namespace-names-std-vs-std

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!