我应该在所有地方使用std:和boost:::前缀吗?

时间:2021-10-04 18:52:48

In my C++ code I don't use the declarations using namespace std; or using namespace boost;. This makes my code longer and means more typing. I was thinking about starting to use the "using" declarations, but I remember some people arguing against that. What is the recommended practice? std and boost are so common there should be no much harm in that?

在我的c++代码中,我不使用名称空间std声明;提高;或者使用名称空间。这使我的代码更长,意味着更多的输入。我想开始使用“使用”声明,但我记得有些人对此持反对意见。推荐的做法是什么?std和boost是如此普遍,应该不会有太大的危害吧?

7 个解决方案

#1


16  

I use using namespace only in C++ files, not in headers. Besides, using hole namespace not needed in most of times. For instance, you could write using boost::shared_ptr or using std::tr1::shared_ptr to easily switch between shared_ptr implementations.

我只在c++文件中使用名称空间,而不是在header中使用。此外,在大多数情况下不需要使用hole命名空间。例如,您可以使用boost: shared_ptr或使用std::tr1::shared_ptr进行编写,以便在shared_ptr实现之间轻松切换。

Sample:

示例:

#include <iostream>

using std::cout;

int main()
{
    cout << "test" << std::endl;
    return 0;
}

#2


15  

Using namespace ... was not invented just for fun.

使用名称空间……不是为了好玩而发明的。

If you have a good reason to use it, then do so (and the frequent use of stuff from these namespaces is the exact good reason). Don't listen to fanatics who tell you everything they don't want to do for obscure reasons themselves is evil.

如果您有一个使用它的好理由,那么就这么做(而且频繁地使用这些名称空间中的东西就是一个很好的理由)。不要听信那些*分子,他们告诉你他们不想做的每一件事都是出于隐晦的原因,他们自己就是邪恶的。

However, a good source for reasoning in these regards is C++ FAQ lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

然而,在这些方面进行推理的一个很好的来源是:http://www.parashift.com/c+ -faq-lite/coding-standard .html# FAQ -27.5

I have read it and still decided to use it like you want to. Now you can make your own informed decision :-)

我已经读过了,但还是决定像你想的那样使用它。现在你可以自己作出明智的决定:-)

#3


6  

My own rules are:

我的规则是:

  • in header files, all names are explicitly qualified e.g. std::string, std::cout etc. at their point of use
  • 在头文件中,所有名称都是显式限定的,例如:std: string, std::cout等等
  • in source files, place using clauses for the commonly used names at the top of the file eg. using std::string;
  • 在源文件中,将常用名称的use子句放在文件的顶部。使用std::string;
  • never use using namespace xxxx; in production code.
  • 不要使用名称空间xxxx;在生产代码。

#4


4  

The fact that the code is cluttered with ::std:: prefixes is indeed annoying when reading the code. However, you want to know what namespace a symbol was in as easily as possible...

代码中充斥着::std::前缀,这一事实在阅读代码时确实很烦人。但是,您希望尽可能轻松地知道符号所在的名称空间……

Now isn't that the IDE's job?

这不是IDE的工作吗?

As long as my IDE doesn't support 'view short typenames', I'm leaning towards using declarations for commonly known symbols (i.e. the STL, boost, ...). Readibility first!

只要我的IDE不支持“查看短的typenames”,我就会倾向于对常见的符号(例如STL, boost,…)使用声明。Readibility先!

#5


4  

One factor to keep in mind is that the std namespace is named this way to make it short. A std:: prefix is only 5 characters, hardly the end of the world. That's unlike .NET's namespaces like System.Collections.Generic. It is designed to be easy to type.

需要记住的一个因素是,std名称空间是这样命名的,以使其简短。前缀只有5个字符,几乎是世界末日。这不同于。net的名称空间,如system . collections.general。它被设计成易于输入。

For that reason, I usually just type out the std prefix. Boost isn't too bad either, so I usually type that out too.

出于这个原因,我通常只输入std前缀。Boost也不算太糟,所以我通常也会输入它。

I usually alias the sub-namespaces (boost::filesystem for example) to something shorter (namespace fs = boost::filesystem for example)

我通常将子名称空间(例如boost:::文件系统)别名为更短的名称空间(例如,名称空间fs = boost::文件系统)

Using typedefs liberally helps too. And if I need to reference a type often, I might just add a using for it.

大量使用typedef也有帮助。如果我需要经常引用一个类型,我可以为它添加一个用途。

But I generally try to avoid using's in headers especially, and when I do use them, I prefer to put them at function scope to avoid polluting the actual namespace.

但是我通常尽量避免使用's在header中,当我使用它们时,我更喜欢将它们放在function scope中,以避免污染实际的名称空间。

C++ offers a lot of tools that let you avoid having to specify the namespace, without polluting the global namespace.

c++提供了许多工具,使您不必指定名称空间,也不会污染全局名称空间。

#6


1  

In header files, yes. That is because using "using std::name_of_std_member;" or using "using namespace std;" in a header file will cause all other files which include that header file to see the symbol in the global scope, thus defeating the purpose of namespaces. In source files, however, it is perfectly ok to use "using namespace std;" to make the symbols of that namespace available with the "std::" prefix.

在头文件中,是的。这是因为在头文件中使用“using std::name_of_std_member;”或使用“using namespace std;”将导致包含头文件的所有其他文件在全局范围中看到符号,从而挫败名称空间的目的。但是,在源文件中,完全可以使用“使用名称空间std;”使该名称空间的符号具有“std::”前缀。

#7


0  

I use using namespace only inside function bodies. In header files, I always qualify the namespace explicitly.

我只在函数体内部使用命名空间。在头文件中,我总是显式地限定名称空间。

Seldom (when copy pasting a colleague's code for mockup), I use using namespace at namespace scope (ie. for the whole translation unit).

我很少(在复制粘贴同事的代码时)使用命名空间作用域(即命名空间作用域)。为整个翻译单位)。

#1


16  

I use using namespace only in C++ files, not in headers. Besides, using hole namespace not needed in most of times. For instance, you could write using boost::shared_ptr or using std::tr1::shared_ptr to easily switch between shared_ptr implementations.

我只在c++文件中使用名称空间,而不是在header中使用。此外,在大多数情况下不需要使用hole命名空间。例如,您可以使用boost: shared_ptr或使用std::tr1::shared_ptr进行编写,以便在shared_ptr实现之间轻松切换。

Sample:

示例:

#include <iostream>

using std::cout;

int main()
{
    cout << "test" << std::endl;
    return 0;
}

#2


15  

Using namespace ... was not invented just for fun.

使用名称空间……不是为了好玩而发明的。

If you have a good reason to use it, then do so (and the frequent use of stuff from these namespaces is the exact good reason). Don't listen to fanatics who tell you everything they don't want to do for obscure reasons themselves is evil.

如果您有一个使用它的好理由,那么就这么做(而且频繁地使用这些名称空间中的东西就是一个很好的理由)。不要听信那些*分子,他们告诉你他们不想做的每一件事都是出于隐晦的原因,他们自己就是邪恶的。

However, a good source for reasoning in these regards is C++ FAQ lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

然而,在这些方面进行推理的一个很好的来源是:http://www.parashift.com/c+ -faq-lite/coding-standard .html# FAQ -27.5

I have read it and still decided to use it like you want to. Now you can make your own informed decision :-)

我已经读过了,但还是决定像你想的那样使用它。现在你可以自己作出明智的决定:-)

#3


6  

My own rules are:

我的规则是:

  • in header files, all names are explicitly qualified e.g. std::string, std::cout etc. at their point of use
  • 在头文件中,所有名称都是显式限定的,例如:std: string, std::cout等等
  • in source files, place using clauses for the commonly used names at the top of the file eg. using std::string;
  • 在源文件中,将常用名称的use子句放在文件的顶部。使用std::string;
  • never use using namespace xxxx; in production code.
  • 不要使用名称空间xxxx;在生产代码。

#4


4  

The fact that the code is cluttered with ::std:: prefixes is indeed annoying when reading the code. However, you want to know what namespace a symbol was in as easily as possible...

代码中充斥着::std::前缀,这一事实在阅读代码时确实很烦人。但是,您希望尽可能轻松地知道符号所在的名称空间……

Now isn't that the IDE's job?

这不是IDE的工作吗?

As long as my IDE doesn't support 'view short typenames', I'm leaning towards using declarations for commonly known symbols (i.e. the STL, boost, ...). Readibility first!

只要我的IDE不支持“查看短的typenames”,我就会倾向于对常见的符号(例如STL, boost,…)使用声明。Readibility先!

#5


4  

One factor to keep in mind is that the std namespace is named this way to make it short. A std:: prefix is only 5 characters, hardly the end of the world. That's unlike .NET's namespaces like System.Collections.Generic. It is designed to be easy to type.

需要记住的一个因素是,std名称空间是这样命名的,以使其简短。前缀只有5个字符,几乎是世界末日。这不同于。net的名称空间,如system . collections.general。它被设计成易于输入。

For that reason, I usually just type out the std prefix. Boost isn't too bad either, so I usually type that out too.

出于这个原因,我通常只输入std前缀。Boost也不算太糟,所以我通常也会输入它。

I usually alias the sub-namespaces (boost::filesystem for example) to something shorter (namespace fs = boost::filesystem for example)

我通常将子名称空间(例如boost:::文件系统)别名为更短的名称空间(例如,名称空间fs = boost::文件系统)

Using typedefs liberally helps too. And if I need to reference a type often, I might just add a using for it.

大量使用typedef也有帮助。如果我需要经常引用一个类型,我可以为它添加一个用途。

But I generally try to avoid using's in headers especially, and when I do use them, I prefer to put them at function scope to avoid polluting the actual namespace.

但是我通常尽量避免使用's在header中,当我使用它们时,我更喜欢将它们放在function scope中,以避免污染实际的名称空间。

C++ offers a lot of tools that let you avoid having to specify the namespace, without polluting the global namespace.

c++提供了许多工具,使您不必指定名称空间,也不会污染全局名称空间。

#6


1  

In header files, yes. That is because using "using std::name_of_std_member;" or using "using namespace std;" in a header file will cause all other files which include that header file to see the symbol in the global scope, thus defeating the purpose of namespaces. In source files, however, it is perfectly ok to use "using namespace std;" to make the symbols of that namespace available with the "std::" prefix.

在头文件中,是的。这是因为在头文件中使用“using std::name_of_std_member;”或使用“using namespace std;”将导致包含头文件的所有其他文件在全局范围中看到符号,从而挫败名称空间的目的。但是,在源文件中,完全可以使用“使用名称空间std;”使该名称空间的符号具有“std::”前缀。

#7


0  

I use using namespace only inside function bodies. In header files, I always qualify the namespace explicitly.

我只在函数体内部使用命名空间。在头文件中,我总是显式地限定名称空间。

Seldom (when copy pasting a colleague's code for mockup), I use using namespace at namespace scope (ie. for the whole translation unit).

我很少(在复制粘贴同事的代码时)使用命名空间作用域(即命名空间作用域)。为整个翻译单位)。