.inl 文件在 C++ 中的意义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1208028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:11:03  来源:igfitidea点击:

Significance of a .inl file in C++

c++

提问by Zuzu

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

在 .inl 文件中声明有什么好处?我什么时候需要使用相同的?

回答by Nick Meyer

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

.inl文件从来都不是强制性的,对编译器没有特殊意义。这只是一种构建代码的方式,可以为可能阅读它的人提供提示。

I use .inlfiles in two cases:

.inl在两种情况下使用文件:

  • 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 #includethe .inlfile at the bottom of the header file.

在这两种情况下,我将函数的声明放在其他文件包含的头文件中,然后#include.inl文件放在头文件的底部。

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 .inlfile and read it. If you don't, you don't have to.

我喜欢它,因为它将接口与实现分开,并使头文件更易于阅读。如果你关心实现细节,你可以打开.inl文件并阅读它。如果你不这样做,你就没有必要。

回答by paercebal

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

Nick Meyer是对的:编译器不关心您包含的文件的扩展名,因此诸如“.h”、“.hpp”、“.hxx”、“.hh”、“.inl”之类的东西, “.inc”等是一个简单的约定,用于明确文件应该包含的内容。

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

最好的例子是没有任何扩展名的 STL 头文件。

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

通常,“.inl”文件确实包含内联代码(因此是“.inl”扩展名)。

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

这些文件“.INL”文件是必须的,当你有之间的依赖周期码。

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:

解决方案是将定义和实现分解为两种头文件:

  • hppfor header declaration/definition
  • inlfor header implementation
  • hpp用于标头声明/定义
  • inl用于标头实现

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.

这样,您可以在自己的源代码中包含您需要的任何“.inl”文件,并且它会起作用。

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

同样,包含文件的后缀名并不重要,重要的是它们的用途。

回答by Andy J Buchanan

Since nobody else has mentioned it:

由于没有其他人提到它:

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

使用 .inl 文件来存储内联函数对于加速编译非常有用。

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.

如果您只在需要声明的地方包含声明 (.h),并且只在需要它们的地方包含内联实现 (.inl)(即可能仅在 .cpp 和其他 .inl 文件中,而不是 .h 的),则它可以有对您的标头依赖项的有益影响。

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

对于具有许多交互类的大型项目,这可能是一个重大胜利。

回答by Michael Burr

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.

根据我的经验,.inl 文件用于定义内联函数。当它们位于 .inl 文件中时,该文件可以包含在标头中以获取内联函数,并可以包含在 .c 文件中以获取常规函数定义。

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.

它们通常与直接的 C 代码一起使用,而不经常与 C++ 代码一起使用,因为所有 C++ 编译器都支持内联函数。

回答by jcoder

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.

我相信这只是包含内联代码的“头”文件的命名约定。这样 .h 文件可以包含定义,而 .inl 文件包含模板所需的内联代码。

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

我不相信除了命名约定之外,还有什么可以使文件的目的变得清晰