154

What are the advantages of having declarations in a .inl file? When would I need to use the same?

6
  • 6
    FWIW, i hate .inl files. Why split up your code more than necessary?
    – Shog9
    Jul 30, 2009 at 17:23
  • 12
    @shog9: To seporate interface from implementation. I have always hated C# and Java file because it so hard to read the interface because of all the messey implementation details. Jul 30, 2009 at 18:49
  • 10
    @Martin - unfortunately C++ gives us a bad combination of both worlds - the interface and part of the implementation in the header, the rest of the implementation in the .cpp file. Even if you avoid inline functions (or put them in .inl files) you have to clutter up the interface with the pesky details of private members unless you're able to religiously use the pimpl idiom. Jul 30, 2009 at 19:48
  • 6
    Yeah, I've never understood the argument that headers separate interface from implementation. They obviously don't. An interface should not contain all the private members.
    – jalf
    Jul 30, 2009 at 21:40
  • 1
    @LokiAstari: To be fair, Java/C# has very good tooling providing the interface outline automatically. One might put it the other way around: In C++ you have to manually solve a problem, which can be fully solved by computers.
    – bluenote10
    Oct 22, 2016 at 11:42

6 Answers 6

180

.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.

6
  • 5
    Indeed, it's mostly about separating interface from implementation. Jul 30, 2009 at 17:41
  • 1
    I've also seen .ipp and .ixx used for inline definitions and .tpp and .txx for template one. Jul 30, 2009 at 18:34
  • 1
    For example, the GNU Standard C++ Library uses .tcc for template implementation files.
    – musiphil
    Jan 3, 2013 at 18:57
  • 3
    @NickMeyer glm uses .hpp and .inl in exactly the same way you've mentioned above. Good to know, thanks for the great answer :)
    – legends2k
    May 2, 2013 at 19:43
  • 1
    So is it like a header? Mar 21, 2020 at 6:49
109

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.

3
  • 9
    This explains the real benefit (or necessity) of separation, and should have been chosen as the answer.
    – musiphil
    Jan 3, 2013 at 18:59
  • 2
    If the function were not inline, you would you standard .cpp file for the implementation part?
    – Bublafus
    Apr 10, 2015 at 20:55
  • 2
    @Bublafus :If the function were not inline, you would you standard .cpp file for the implementation part? : Possibly. Templates are examples of code that cannot usually be hidden in .CPP files, so in that case, the .INL file would be mandatory.
    – paercebal
    Apr 11, 2015 at 21:21
39

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.

5
  • 10
    +1: the world is definitely a different place when you are managing millions of lines of code and thousands of files.
    – gatorfax
    Jul 30, 2009 at 21:29
  • so you should never include .inl in header files? I always had the feeling that .inl should be put on the bottom of header files since inline functions require declaration and implementation to be reachable at once. May 20, 2013 at 17:51
  • 2
    Icebone1000 not all modules that include the header necessarily want to use the inline functions, so they have no need for the implementations to be read in, they're not required to be present if they're not used. Jun 26, 2013 at 14:57
  • 1
    @Nikos I think he meant faster relative to putting all your inline functions in your header files. Jun 4, 2019 at 20:21
  • Thank you, some of the other answers imply that .inl files were useless. Obviously they are invaluable in enterprise level projects.
    – in-just
    Oct 3, 2022 at 0:12
4

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.

2
  • I don't see the point in doing this just to get C support. For C, you'd just conditionally #define inline static, and define your inline functions in the header. Jul 30, 2009 at 17:41
  • I guess this avoids multiple copies of the same function from ending up in the binary. I'm just saying I've seen .inl files used this way, not that it's the only technique (or even best). Jul 30, 2009 at 17:49
1

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

0

Andy J Buchanan's answer is very important. Software design based on .inl files etc goes beyond maintainability and readability. As Item 31 of Effective C++ by Scott Meyers points out, it is important to "minimize compilation dependencies between files".

The key to this seperation is replacement of dependencies on definitions with dependencies on declarations.

Avoid using objects when object references and pointers will do.

Depend on class declarations instead of class definitions whenever you can.

Provide separate header files for declarations and definitions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.