Defining a write function in header files in C ++

I have a class that has many small functions. I call small functions functions that do not perform any processing, but simply return a literal value. Something like:

string Foo::method() const{ return "A"; } 

I created the header file "Foo.h" and the source file "Foo.cpp". But since the function is very small, I am thinking of placing it in the header file itself. I have the following questions:

  • Is there any performance or other issues if I put this function definition in the header file? I will have many such features.
  • As far as I understand, when the compilation is complete, the compiler will expand the header file and place it where it is included. It is right?
+54
c ++ performance header-files
Jan 17 '09 at 14:36
source share
7 answers

If the function is small (the likelihood that you change it often is often small), and if the function can be placed in the header without including a myriad of other headers (because your function depends on them), this is absolutely true for Do it. If you declare them extern inline, then the compiler must provide it with the same address for each compilation unit:

headera.h:

 inline string method() { return something; } 

Member functions are implicit built-in if defined inside their class. The same is true for them: if they can be placed in the headline without the hassle, you really can do it.

Since the function code is placed in the header and visible, the compiler can embed calls into them, that is, put the function code directly on the call site (not so much because you inserted it before it, but more because the compiler solves this way, though. Entering inline only hints for the compiler regarding this). This can lead to increased performance, because the compiler now sees where the arguments correspond to variables local to this function, and where the argument is not aliased to each other - and, last but not least, the assignment of function frames is no longer required.

As far as I understand, when the compilation is complete, the compiler will expand the header file and place it where it is included. It is right?

Yes, it's right. The function will be defined in all places where you include the header. The compiler will take care of placing only one instance in the resulting program, excluding the rest.

+61
Jan 17 '09 at 14:44
source share

Depending on your compiler and its settings, it can perform any of the following actions:

  • It can ignore the inline keyword (it is just a hint to the compiler, not a command) and create standalone functions. He can do this if the functions exceed the compiler threshold of complexity. for example A lot of nested loops.
  • It may decide what your standalone function is a good candidate for inline extension.

In many cases, the compiler is in a much better position to determine if a function should be nested than you, so there is no point in being second-rate. I like to use implicit nesting when a class has many small functions just because itโ€™s convenient to have an implementation right there in the class. This does not work well for large functions.

Another thing to keep in mind is that if you export the class to a DLL / shared library (IMHO is not a big idea, but people do it anyway), you need to be very careful with the built-in functions. If the compiler that built the DLL decides that the function should be inlined, you have several potential problems:

  • The compiler creating the program using the DLL may decide not to embed the function so that it generates a symbol reference for a function that does not exist and the DLL does not load.
  • If you update the DLL and change the built-in function, the client program will still use the old version of this function, since the function got into the client code.
+12
Jan 17 '09 at 15:18
source share

Performance will increase as the implementation in the header files is implicitly embedded. Since you mentioned that your functions are small, the built-in operation will be so useful for you IMHO.

What you say about the compiler is also true. There is no difference for the compiler - except that embedding is between the code in the header file or .cpp .

+4
Jan 17 '09 at 14:41
source share
  • If your functions are so simple, make them inline, and you still have to embed them in the header file. In addition, any agreements are only those agreements.

  • Yes, the compiler extends the header file, where it encounters #include statements.

+2
Jan 17 '09 at 14:42
source share

It depends on the coding standards that apply in your case, but:

Small functions without loops and everything else should be built in to improve performance (but slightly larger code is important for some limited or embedded applications).

If you have the function body in the header, you will have it inline (d) by default (which is good when it comes to speed).

Before the compiler creates the object file, the preprocessor is called (the -g option for gcc), and the result is sent to the compiler, which creates the object from the code.

So a shorter answer:

- Declaring functions in the header is useful for speed (but not for space) -

+1
Jan 17 '09 at 14:49
source share

You must use the built-in functions. Read this built-in feature for a better understanding and compromise.

+1
Jan 17 '09 at 15:17
source share

C ++ will not complain if you do this, but generally speaking you shouldn't.

when you # include the file, the entire contents of the included file are inserted at the time of inclusion. This means that any definitions that you put in your header are copied to every file containing that header.

For small projects this is unlikely to be a problem. But for larger projects, this can take a long time to compile (since the same code is recompiled every time it occurs) and can significantly inflate the size of your executable. If you make changes to the definition in a code file, only this .cpp file needs to be recompiled. If you make changes to the definition in the header file, each code file that includes the header must be recompiled. One small change may make it necessary to recompile the entire project!

Sometimes trivial functions that are unlikely to change are excluded (for example, if the function definition is one line).

+1
Dec 05 '16 at 5:57
source share



All Articles