何时使用"::" C ++中的全局范围? [英] When to use "::" for global scope in C++?

查看:83
本文介绍了何时使用"::" C ++中的全局范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每隔一段时间,我会偶然发现一些我正在维护的代码,这对我思考代码样式的方式提出了挑战.今天是那些日子之一...

Every once in a while, I stumble across some code that I'm maintaining that challenges the way I think about my code style. Today was one of those days...

我知道您为什么要使用范围运算符定义全局范围的原因.实际上,这里没有范围的范围分辨率运算符是一个很棒的链接告诉你为什么.

I'm aware that about why you would want to use the scope operator to define global scope. In fact, here scope resolution operator without a scope is a great link tell you why.

但是,我看到了一些让我今天思考的东西.所有有问题的类都包装在项目的名称空间中(很好!),但是我确实看到了全局作用域运算符的大量用法.也就是说,它用于C库中的所有内容(uint8_t等除外).是的,程序员使用了该库的.h版本,因为显然他们所运行的g ++版本仍然对新C ++发出警告.标准).这有用吗?我认为这只是浪费字符(提醒我使用this指针...,但对于复制构造函数和赋值运算符除外,因为它有助于阐明哪个对象是哪个对象).我想念什么吗?当然,有人可以按照usleep()或stderr(我在其中最常使用"::"的方式)的名字来命名,但是他们不知道这样做可能会破坏某些东西吗?您在什么时候用范围运算符说拧"它,只是告诉自己,有人在您的命名空间中以某种方式命名一个函数会带来麻烦?

However, I saw something that made me think today. All the classes in question were wrapped into a namespace for the project (good!), but I did see copious usage of the global scope operator. Namely, it was used for everything from C libraries (with the exception of uint8_t and the like... yes, the programmer used the .h version of this library since apparently the version of g++ they were running still threw warnings about the new C++ standard). Is this useful? I view this as just as a waste of characters (reminds me of using the this pointer... except in the case of the copy constructor and assignment operator where it helps in clarifying which object is which). Am I missing something? Sure, someone can come around and name something along the lines of usleep() or stderr (where I saw the most usage of "::"), but won't they know that doing so will probably break something horribly? At what point do you say "screw it" in terms of the scope operator and just tell yourself that someone who names a function a certain way within your namespace is asking for trouble?

所以我的问题是...在这种情况下使用全局范围运算符的正确"(我理解的主观)方式是什么? std或您自己的命名空间中未包括的所有内容都应该明确定义全局范围吗?我倾向于谨慎行事,并使用"std ::"来避免using指令,但是在这里使用global作用域运算符有什么好处吗?为了清晰起见,我倾向于认为它确实导致以下事实:我们没有从当前名称空间获取有问题的变量或函数,但是我在包括它和没有给出当前的发展之间感到困惑.

So my question is... what is the "correct" (subjective I understand) way of using the global scope operator in this context? Should everything not included in std or your own namespaces have the global scope explicitly defined? I tend to err on the side of caution and use "std::" to avoid the using directive, but is anything gained from using the global scope operator here? I tend to think for clarity's sake it does lend to the fact that we aren't getting the variable or function in question from the current namespace, but I'm torn between including it and not given today's developments.

一如既往,感谢您提供的帮助和指导,以使我的代码更清晰,可读性更好(当然)也更加出色.

As always, thanks for the help and guidance as I look to make my code cleaner, more readable, and (of course) significantly more awesome.

推荐答案

我很少使用它;仅当出于某种原因需要解决一些歧义时.不过,这是非常主观的.

I use it quite infrequently; only when some ambiguity needs resolving for whatever reason. This is pretty subjective, though.

在某些情况下(例如,在模板内部),您担心仅在某些情况下ADL会引起歧义:

There may be some occasions (say, inside a template) where you're worried about ADL causing ambiguities only in certain cases:

template <typename T>
void foo(T t)
{
   ::bar(t);  // :: just in case there's a `bar` in `T`'s namespace
}

这篇关于何时使用"::" C ++中的全局范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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