何时使用“::"对于 C++ 中的全局范围?

When to use quot;::quot; for global scope in C++?(何时使用“::对于 C++ 中的全局范围?)

本文介绍了何时使用“::"对于 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::"来避免使用指令,但是在这里使用全局范围运算符有什么好处吗?为了清楚起见,我倾向于认为这确实说明了我们没有从当前命名空间中获取有问题的变量或函数,但我在包括它和没有给出今天的发展之间左右为难.

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++ 中的全局范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:何时使用“::"对于 C++ 中的全局范围?

基础教程推荐