在名称空间“名称"上::: std :: vs std :: [英] On namespace 'names': ::std:: vs std::

查看:94
本文介绍了在名称空间“名称"上::: std :: vs std ::的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这里查看Stackoverflow上的一些帖子,并且我注意到大多数人使用std::,但有些人使用::std::

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

我认为我已经读过一些关于 global scope 或类似名称空间的内容,作为使用::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?

推荐答案

编写这样的代码是一个坏主意,但是您可以:

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

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

在这种情况下,std::string是指::foo::std命名空间,而不是::std命名空间.因此,使用::std::string会更加明确一些.

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.

这篇关于在名称空间“名称"上::: std :: vs std ::的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆