是否所有使用指令看法与使用命名空间std相同? [英] Are all using directives viewed the same way as using namespace std?

查看:234
本文介绍了是否所有使用指令看法与使用命名空间std相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很容易习惯使用 std :: 为标准标识符加上前缀,而不是在中使用namespace std; 。但是,我开始进入C#,我注意到,添加任何使用指令是必要的,这是很正常的,你会看到:

I've easily gotten myself into the habit of prefixing standard identifiers with std:: instead of sticking in a using namespace std;. However, I've started getting into C# and I've noticed that it's very normal to add in whatever using directives are necessary, i.e., you'd see:

using System;
Console.Write("foo");

而不是:

System.Console.Write("foo");

显然,正如我从这个主题的C#问题中发现的, C#中的单个系统命名空间远远小于 std 在C ++中,因此消除了与名称冲突相关的问题,因为存在更少的可能性(您可以如果库使用名称冲突更新,则使用完全限定名称来查找替换它),并且消除了与显示的Intellisense选项的crapload相关联的问题,因为命名空间足够小以处理。

Apparently, as I found out from a C# question on this topic, that usage comes from the fact that individual system namespaces in C# are much, much smaller than std is in C++, and therefore eliminates the problems associated with name clashes, as there is much less likelihood (and you can just find-replace it with a fully qualified name if a library updates with a name-clash), and eliminates the problems associated with a crapload of Intellisense options appearing, as the namespaces are small enough to handle.

那么,问题是,如果这些是使用C#中使用指令的常见原因,C ++是否也是如此?通常可以将其应用于较小的第三方命名空间以及您自己的较小命名空间吗?

The question, then, is that if these are the standing reasons to make use of using directives in C#, is the same true for C++? Is it generally acceptable to apply this to smaller third-party namespaces, as well as your own smaller namespaces?

现在我意识到这可能会引起一些争议,我想借这个时刻要求它不会变成一个参数。一个好的答案应该包括一个基础,即优点或缺点,以及如何使用一个方式对另一个真的产生有价值的影响。

Now I realize that this might cause a bit of controversy, I want to take this moment to ask that it doesn't turn into an argument. A good answer should include a basis, i.e., advantages or disadvantages, and how using one way over the other really makes a worthwhile difference.

我问这个问题的原因是清除这个问题,并可能删除的观念,使用C ++中的指令必须是一件坏事。当然,如果需要,可以使用命名空间别名来缩短命名空间名称,并且如果需要,仍然可以使用完全限定的名称,但是有时一个使用指令可以极大地方便访问一些成员,例如用户定义的文字操作符,据我所知,没有任何形式的ADL,这意味着您必须使用using指令,或者通过函数语法调用运算符方法,首先打败了使用运算符的整个目的。

The reason I ask this is to clear up the issue, and possibly remove the notion that using directives in C++ have to be a bad thing. Sure longer namespace names can be cut down with a namespace alias if necessary, and fully qualified names can still be used if needed, but sometimes a using directive greatly eases accessing some members such as user-defined literal operators, which, to my knowledge, have no form of ADL, meaning that you either have to use a using directive, or call the operator method by the function syntax, defeating the whole purpose of using the operator in the first place.

例如,我有一个命名空间(包括一个表示键盘键的结构,以及一个可读的替代访问方式的后缀:

For example, I had a namespace (that includes a structure representing a keyboard key, along with a literal suffix as a readable alternate means of access:

"caps lock"_key.disable();

这里是,除非你以前使用Whatever :: operator_key; 插入使用命名空间Whatever;

The problem here is that unless you have previously inserted using namespace Whatever; or using Whatever::operator"" _key;, the code won't compile, which is bad news for the user.

使用指令在 std 时会出现明显的问题涉及或当以这样的方式在头部中使用时,它们为该头部的用户带来不需要的额外,但是当包含在比包括头部的更小的范围内时,对于其他命名空间是否合理地使用它们?由于不需要每次都输入每个限定符,所以保存的击键功能,以及今天的Intellisense功能,找出一个非限定标识符属于哪个命名空间就像鼠标悬停一样简单。

Using directives have obvious problems when std is involved or when used in such a way in a header that they bring unwanted extras for the user of that header, but is it justified to use them for other namespaces when contained within a smaller scope than whatever includes a header? The keystrokes saved from not having to type each qualifier each time do add up, and with today's Intellisense capabilities, finding out which namespace an unqualified identifier belongs to is as easy as mousing over it.

推荐答案

我认为在C ++中使用声明的规则很简单:

I think the rules about using declarations in C++ are rather simple:


  1. 写使用命名空间任何东西;在头部的全局范围中,特别是可能被其他程序(例如,当写入库时)重用的头部。它会使用此命名空间的符号来污染所有后续标头的全局范围,从而破坏命名空间的整体用途,并可能在以后创建不可预见的名称冲突。

  2. 在.cpp文件和头文件的内部范围(例如内联函数范围),你可以使用任何你想要的命名空间,因为它不会影响任何其他文件。

编辑:对于_key问题,只需定义这个操作符在自己的命名空间中并告诉用户导入它。这样,他们不需要输入运算符声明。

for the _key problem, simply define this operator in its own namespace and tell users to import it. This way they won't need to type out the operator declaration.

namespace something {
class key { ... };
}
namespace key_suffix {
something::key operator"" _key() { ... }
}

// user code
void some_function() {
    using namespace key_suffix;
    "caps lock"_key.doSomething();
}

这篇关于是否所有使用指令看法与使用命名空间std相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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