Should I repeat "#include" and "using" in both header and implementation files (C ++)?

I am new to C ++, but I understand that the #include statement essentially just removes the contents of the #included file to the location of this statement. This means that if there are a number of "#include" and "using" statements in my header file, my implementation file can simply # include the header file, and the compiler will not mind if I do not repeat other statements.

What about people?

My main problem is that if I do not repeat the statements '#include', 'using', as well as 'typedef' (now when I think about it), it takes this information away from the file in which it can lead to confusion.

I am just working on small projects at a time when it will not cause any problems, but I can imagine that in large projects with a large number of people working on them, this can become a serious problem.

Example:

UPDATE: the prototypes of my functions for 'Unit' have strings, ostream and StringSet among their types and return parameters. I do not include anything in my header file, which is used only in the implementation file.

//Unit.h #include <string> #include <ostream> #include "StringSet.h" using std::string; using std::ostream; class Unit { public: //public members with string, ostream and StringSet //in their return values/parameter lists private: //private members //unrelated side-question: should private members //even be included in the header file? } ; //Unit.cpp #include "Unit.h" //The following are all redundant from a compiler perspective: #include <string> #include <ostream> #include "StringSet.h" using std::string; using std::ostream; //implementation goes here 
+6
c ++ include header-files using-statement
source share
5 answers

A pointer directive ( using namespace std; ) should not be in the header if it is not contained within the function. This is bad practice. It is unlikely that every user of your title wants to receive an unqualified search for everything in a given namespace; including unrelated headers can lead to unexpected ambiguities and compilation failures. Personally, I avoid the use directive inside functions for the same reasoning, but this is usually considered less harmful.

You should carefully use a type alias (either through typedef std::string string; or using string = std::string; ). Type definitions make sense, so it should never be updated. For example, this is an error:

 typedef int myint; typedef float myint; 

due to conflicting types.

Using declarations ( using std::string; or using std::memcpy; ) makes the character available for unqualified name lookups . This is extremely useful if you use the argument-dependent search correctly, which usually does not matter if you are not writing a library. The tip differs depending on whether you enter a type or function. Think about how to use type declarations in the same way as type aliases. It does not make sense to have several definitions under the same name. With functions, all you really do is extend your overload resolution to include a few more things (although this is usually not necessary).

 // Finding multiple operator<< functions makes sense using std::operator<<; using mylib::operator<<; // Finding multiple string classes does not make sense using std::string; using mylib::string; 

To repeat #include you should consider whether you really need to include the file in the header first. An advanced declaration may be appropriate for your needs.

+6
source share
  • Include in the header / source what you really need (if redirection is available and sufficient, then forward declare instead of including)
  • Do not use the using statement in headers (except inside function areas) ... Adding using to the header will pollute the namespaces of all sources, including the header.
  • You must make sure that every file (source header) includes everything that it needs, and no more.

You do not need to take care that some of them are redundant. You have header protection and precompiler optimization.

You should be able to manipulate each file in isolation.

For example, let's say you use std::string in the header and in the source, but as “optimization”, you only included the string in the header ... If you open later, you will not need more lines in the header and want to delete it (clearing code and that’s all ...), you will need to change the source to include the line. Now imagine that you have TEN sources, including the title ...

Now, of course, you can have exceptions to this rule (for example, precompiled headers or even headers, the only goal is to make a few inclusions as a courtesy), but by default you should have a self-contained header and source files (then there are files containing everything that they use, no more).

+3
source share

Keep header files to a minimum. This means that this is unlikely..cpp file usually includes the appropriate header, as well as any other headers necessary for implementation.

0
source share

As Travis said, you should not have using statements in the header file because this means that they will be included in all translation units that include this header file, which can cause confusing problems.

If I only need the function from the header file in the cpp file, I only include it in this cpp file. This is good practice for larger projects because it means less work for the compiler. Also, wherever possible, I use forward declarations in the headers instead of the inbound ones (and again include the headers in the cpp file).

0
source share

He believed that the using statement in the header file generally has a bad form, unless you intentionally duplicate a character in another namespace. This is normal to use in a cpp file.

Each typedef should exist only once in your code base. It should be in the header file if it should be used in multiple cpp / h files. Duplicating them will cause you a lot of grief.

The header file should contain all the #include statements that it needs, and others. If only class pointers are mentioned, then use a forward declaration, not a heading. Any other functions that are only required inside the cpp file should go there. Repeating inclusions from the header is OK, but not required. It is just a style choice.

0
source share

All Articles