I am a C++ newcomer, trying to learn the language in parallel as I work on a project that requires it. I am using a fairly popular and stable open source library to do a lot of heavy lifting. Reading through the source, tutorials and code samples for the library, I have noticed that they always use fully qualified names when declaring types, which often results in very long and verbose lines with lots of ::'s. Is this considered best practice in C++? Is there a different way to deal with this?
我是一个c++新手,在处理一个需要它的项目时,我试图同时学习这门语言。我正在使用一个相当流行和稳定的开放源码库来做很多繁重的工作。在阅读该库的源代码、教程和代码示例时,我注意到它们在声明类型时总是使用完全限定的名称,这通常导致非常长的、冗长的行,其中包含许多:'s。这被认为是c++的最佳实践吗?有不同的处理方法吗?
3 个解决方案
#1
8
They may have found it easier than answering lots of questions from people who tried the example code and found it didn't work, just because they didn't "use" the namespaces involved.
他们可能发现,这比回答那些尝试过示例代码的人的许多问题更容易,他们发现这并没有用,只是因为他们没有“使用”所涉及的名称空间。
Practices vary - if you're working on a large project with lots of diverse libraries and name *es, you may wish to proactively use more namespace qualifiers consistently so that as you add new code you won't have to go and make old code more explicit about what it's trying to use.
实践各不相同——如果你工作在一个大的项目有很多不同的库和名称冲突,您可能希望主动使用更多的命名空间限定符一致,这样当您添加新代码你不必去做旧代码更明确的关于试图使用。
Stylistically, some people prefer knowing exactly what's being referred to to potentially having to dig around or follow an IDE "go to declaration" feature (if available), while other people like concision and to see fuller namespace qualification only on the "exceptional" references to namespaces that haven't been included - a more contextual perspective.
在文体上,有些人喜欢知道什么被称为潜在的周围挖或遵循一个IDE“去宣言”功能(如果有的话),而其他的人喜欢简洁,只看到完整名称空间资格的“特殊”名称空间的引用,还没有包括上下文的角度。
It's also normal to avoid having "using namespace xxx;" in a header file, as client code including that header won't be able to turn it off, and the contents of that namespace will be permanently dumped into their default "search space". So, if you're looking at code in a header that's one reason they might be more explicit. Contrasting with that, you can having "using namespace" inside a scope such as a function body - even in a header - and it won't affect other code. It's more normal to use an namespace from within an implementation file that you expect to be the final file in a translation unit, compiling up to a library or object that you'll link into the final executable, or perhaps a translation unit that itself creates the executable.
避免在头文件中使用“使用名称空间xxx”也是很正常的,因为包括头文件在内的客户端代码无法关闭它,并且该名称空间的内容将永久地转储到它们的默认“搜索空间”中。所以,如果你在标题中查看代码,这就是为什么它们可能更明确的原因之一。与此相反,您可以在范围(如函数体)中“使用名称空间”—甚至在header中—并且不会影响其他代码。在一个实现文件中使用一个名称空间是比较正常的,您希望将其作为翻译单元中的最终文件,编译成一个库或对象,您将链接到最终的可执行文件,或者可能是一个自己创建可执行文件的翻译单元。
#2
5
First typedefs:
第一个类型定义:
typedef std::vector<MyTypeWithLongName>::const_iterator MyTypeIt;
//use MyTypeIt from now on
Second "using"
第二个“使用”
using std::string;
//use string instead of std::string from now on
Third "using namespace"
第三个“使用名称空间”
using namespace std;
//Use all things from std-namespace without std:: in front (string, vector, sort etc.)
For the best practice: Don't use 'using' and 'using namespace' a lot. When you have to use it (sometimes keeps the code cleaner) never put it in the header but in the .cpp file. I tend to use one of those above if the names get really long or I have to use the types a lot in the same file.
最好的做法是:不要经常使用“使用”和“使用名称空间”。当你必须使用它(有时保持代码的整洁)时,不要把它放在头文件中,而是放在.cpp文件中。如果名称太长,或者必须在同一个文件中大量使用类型,我倾向于使用上面提到的其中之一。
#3
2
If you are writing your own libraries you will certainly have heavy use of namespaces, In your core application there should be fewer uses. As for doing something like std::string
instead of starting with using namespace std;
imo the first version is better because It is more descriptive and less prone to errors
如果您正在编写自己的库,那么您肯定会大量使用名称空间,在您的核心应用程序中,应该会有更少的使用。对于类似std::string这样的操作,而不是从使用命名空间std开始;在我看来,第一个版本更好,因为它更具有描述性,更不容易出错
#1
8
They may have found it easier than answering lots of questions from people who tried the example code and found it didn't work, just because they didn't "use" the namespaces involved.
他们可能发现,这比回答那些尝试过示例代码的人的许多问题更容易,他们发现这并没有用,只是因为他们没有“使用”所涉及的名称空间。
Practices vary - if you're working on a large project with lots of diverse libraries and name *es, you may wish to proactively use more namespace qualifiers consistently so that as you add new code you won't have to go and make old code more explicit about what it's trying to use.
实践各不相同——如果你工作在一个大的项目有很多不同的库和名称冲突,您可能希望主动使用更多的命名空间限定符一致,这样当您添加新代码你不必去做旧代码更明确的关于试图使用。
Stylistically, some people prefer knowing exactly what's being referred to to potentially having to dig around or follow an IDE "go to declaration" feature (if available), while other people like concision and to see fuller namespace qualification only on the "exceptional" references to namespaces that haven't been included - a more contextual perspective.
在文体上,有些人喜欢知道什么被称为潜在的周围挖或遵循一个IDE“去宣言”功能(如果有的话),而其他的人喜欢简洁,只看到完整名称空间资格的“特殊”名称空间的引用,还没有包括上下文的角度。
It's also normal to avoid having "using namespace xxx;" in a header file, as client code including that header won't be able to turn it off, and the contents of that namespace will be permanently dumped into their default "search space". So, if you're looking at code in a header that's one reason they might be more explicit. Contrasting with that, you can having "using namespace" inside a scope such as a function body - even in a header - and it won't affect other code. It's more normal to use an namespace from within an implementation file that you expect to be the final file in a translation unit, compiling up to a library or object that you'll link into the final executable, or perhaps a translation unit that itself creates the executable.
避免在头文件中使用“使用名称空间xxx”也是很正常的,因为包括头文件在内的客户端代码无法关闭它,并且该名称空间的内容将永久地转储到它们的默认“搜索空间”中。所以,如果你在标题中查看代码,这就是为什么它们可能更明确的原因之一。与此相反,您可以在范围(如函数体)中“使用名称空间”—甚至在header中—并且不会影响其他代码。在一个实现文件中使用一个名称空间是比较正常的,您希望将其作为翻译单元中的最终文件,编译成一个库或对象,您将链接到最终的可执行文件,或者可能是一个自己创建可执行文件的翻译单元。
#2
5
First typedefs:
第一个类型定义:
typedef std::vector<MyTypeWithLongName>::const_iterator MyTypeIt;
//use MyTypeIt from now on
Second "using"
第二个“使用”
using std::string;
//use string instead of std::string from now on
Third "using namespace"
第三个“使用名称空间”
using namespace std;
//Use all things from std-namespace without std:: in front (string, vector, sort etc.)
For the best practice: Don't use 'using' and 'using namespace' a lot. When you have to use it (sometimes keeps the code cleaner) never put it in the header but in the .cpp file. I tend to use one of those above if the names get really long or I have to use the types a lot in the same file.
最好的做法是:不要经常使用“使用”和“使用名称空间”。当你必须使用它(有时保持代码的整洁)时,不要把它放在头文件中,而是放在.cpp文件中。如果名称太长,或者必须在同一个文件中大量使用类型,我倾向于使用上面提到的其中之一。
#3
2
If you are writing your own libraries you will certainly have heavy use of namespaces, In your core application there should be fewer uses. As for doing something like std::string
instead of starting with using namespace std;
imo the first version is better because It is more descriptive and less prone to errors
如果您正在编写自己的库,那么您肯定会大量使用名称空间,在您的核心应用程序中,应该会有更少的使用。对于类似std::string这样的操作,而不是从使用命名空间std开始;在我看来,第一个版本更好,因为它更具有描述性,更不容易出错