Should I use std :: and boost :: prefixes everywhere?

In my C ++ code, I do not use declarations using namespace std; or using namespace boost; . This makes my code longer and means more input. I was thinking about starting to use the “use” of declarations, but I remember how some people argued with this. What is the recommended practice? Are std and boost so common that there shouldn't be much harm?

+7
c ++
source share
7 answers

I use using namespace only in C ++ files, not in headers. In addition, the use of the hole namespace is not required in most cases. For example, you can write using boost::shared_ptr or using std::tr1::shared_ptr to easily switch between shared_ptr implementations.

Example:

 #include <iostream> using std::cout; int main() { cout << "test" << std::endl; return 0; } 
+16
source share

Using namespace ... not invented just for fun.

If you have good reason to use it, do it (and the frequent use of materials from these namespaces is an accurate good reason). Do not listen to fanatics who tell you everything that they do not want to do for dark reasons on their own, this is evil.

However, a good argument in this regard is the C ++ FAQ lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

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

+15
source share

My own rules:

  • in header files, all names are explicitly specified, for example. std::string , std::cout , etc. when using them
  • in the source files, place using sentences for commonly used names at the top of the file, for example. using std::string;
  • never use using namespace xxxx; in the production code.
+6
source share

The fact that the code is cluttered with :: std :: prefixes is really annoying when reading the code. However, you want to know which namespace was as simple as possible ...

Isn't this an IDE job?

As long as my IDE does not support "looking for short names", I tend to use using declarations for well-known characters (i.e. STL, boost, ...). Read it first!

+4
source share

One factor to keep in mind is that the std is called in such a way as to make it short. The prefix A std:: is only 5 characters, hardly the end of the world. This is in contrast to .NET namespaces such as System.Collections.Generic . It is designed for easy typing.

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

Usually I usually call sub-name spaces ( boost::filesystem for example) something shorter ( namespace fs = boost::filesystem for example)

Using typedefs also helps. And if I need to refer to a type often, I can just add using for it.

But I generally try to avoid using in headers, especially when I use them, I prefer to put them in the function area so as not to pollute the actual namespace.

C ++ offers many tools that let you not specify a namespace without polluting the global namespace.

+4
source share

In the header files, yes. This is because using "using std :: name_of_std_member;" or using "using namespace std;" in the header file, it will call all other files that include this header file to see the character in the global scope, thereby defeating the namespace target. However, it is normal to use "using namespace std;" in the source files. so that the characters in this namespace are accessible with the prefix "std ::".

+1
source share

I use using namespace only inside function bodies. In header files, I always explicitly define a namespace.

Rarely (when copying pasting peer code for layout) I use using namespace in the using namespace area (i.e. for the whole translation unit).

0
source share

All Articles