Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Significance of a .inl file in C++

Tags:

c++

People also ask

What is an .INL file?

Source code file used by Microsoft C++ compilers; contains inline functions that can be included into C++ programs; used for storing reusable program components. INL files can be used as an alternative to placing implementations of inline functions in header files (. H or . HPP files).

Why are header files used?

Why Do You Use Header Files? Header files are used in C++ so that you don't have to write the code for every single thing. It helps to reduce the complexity and number of lines of code. It also gives you the benefit of reusing the functions that are declared in header files to different .

What is inline CPP?

Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well.


.inl files are never mandatory and have no special significance to the compiler. It's just a way of structuring your code that provides a hint to the humans that might read it.

I use .inl files in two cases:

  • For definitions of inline functions.
  • For definitions of function templates.

In both cases, I put the declarations of the functions in a header file, which is included by other files, then I #include the .inl file at the bottom of the header file.

I like it because it separates the interface from the implementation and makes the header file a little easier to read. If you care about the implementation details, you can open the .inl file and read it. If you don't, you don't have to.


Nick Meyer is right: The compiler doesn't care about the extension of the file you're including, so things like ".h", ".hpp", ".hxx", ".hh", ".inl", ".inc", etc. are a simple convention, to make it clear what the files is supposed to contain.

The best example is the STL header files which have no extension whatsoever.

Usually, ".inl" files do contain inline code (hence the ".inl" extension).

Those files ".inl" files are a necessity when you have a dependency cycle between header code.

For example:

// A.hpp
struct A
{
    void doSomethingElse()
    {
       // Etc.
    }

    void doSomething(B & b)
    {
       b.doSomethingElse() ;
    }
} ;

And:

// B.hpp
struct B
{
    void doSomethingElse()
    {
       // Etc.
    }

    void doSomething(A & a)
    {
       a.doSomethingElse() ;
    }
} ;

There's no way you'll have it compile, including using forward declaration.

The solution is then to break down definition and implementation into two kind of header files:

  • hpp for header declaration/definition
  • inl for header implementation

Which breaks down into the following example:

// A.hpp

struct B ;

struct A
{
    void doSomethingElse() ;
    void doSomething(B & b) ;
} ;

And:

// A.inl
#include <A.hpp>
#include <B.hpp>

inline void A::doSomethingElse()
{
   // Etc.
}

inline void A::doSomething(B & b)
{
   b.doSomethingElse() ;
}

And:

// B.hpp

struct A ;

struct B
{
    void doSomethingElse() ;
    void doSomething(A & a) ;
} ;

And:

// B.INL
#include <B.hpp>
#include <A.hpp>

inline void B::doSomethingElse()
{
   // Etc.
}

inline void B::doSomething(A & a)
{
   a.doSomethingElse() ;
}

This way, you can include whatever ".inl" file you need in your own source, and it will work.

Again, the suffix names of included files are not really important, only their uses.


Since nobody else has mentioned it:

The use of .inl files to store your inline functions can be useful for speeding up compiles.

If you only include the declarations (.h) where you need declarations, and only include inline implementations (.inl) where you need them ( i.e. probably only in .cpp and other .inl files, not .h's ), it can have a beneficial effect on your header dependencies.

This can be a significant win on larger projects with many interacting classes.


In my experience, .inl files are used to define inline functions. When they're in an .inl file, the file can be included in a header to get inline functions and in a .c file to get regular function definitions.

This way the same source can more easily work with compilers that do not have inline function supportas well as compilers that do.

They're usually used with straight C code, not often with C++ code as all C++ compilers support inline functions.


I believe it's just a naming convention for a "header" file includes inline code. it's so that .h files can contain definitions and .inl files contain inline code which is necessary for templates.

I don't belive there is anything more to it than an naming convention to make the purpose of the file clear