内联函数原型 vs 常规声明 vs 原型

发布于 2024-12-12 10:42:25 字数 449 浏览 0 评论 0原文

内联函数和像这样的 main 之间有什么区别:

inline double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

vs 只是定期声明一个函数,例如:

double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

vs 函数原型?

double cube(double);

int main( )
{
    cube(5);
}

double cube(double side)
{
   return side * side * side;
}

What's the difference between inline function and then main like so:

inline double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

vs just declaring a function regularly like:

double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

vs function prototype?

double cube(double);

int main( )
{
    cube(5);
}

double cube(double side)
{
   return side * side * side;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇俏 2024-12-19 10:42:25

内联函数可以在多个翻译单元(cpp文件+包含)中定义,并且是编译器内联该函数的提示。它通常放置在标头中,这会增加编译时间,但可以提高代码速度。它还允许从许多编译单元使用该函数。

//cube.h
inline double cube(double side)
{
   return side * side * side;
}

//cube.cpp
int main( )
{
    cube(5);
}

定期定义它是正常的方法,它(通常)在 cpp 文件中定义并链接。它不容易从其他编译单元中使用。

//cube.cpp
double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

原型允许您告诉编译器一个函数将在链接时存在,即使它还不存在。这允许 main 调用该函数,即使该函数尚不存在。通常,原型位于标头中,因此其他编译单元可以调用该函数,而无需自己定义它。这具有最快的编译时间,并且该函数很容易从其他编译单元使用。

//cube.h
double cube(double);

//cube.cpp
int main( )
{
    cube(5);
}

double cube(double side)
{
   return side * side * side;
}

An inline function can be defined in multiple translation units (cpp file + includes), and is a hint to the compiler to inline the function. It is usually placed in a header which increases compile time, but can lead to faster code. It also allows the function to be used from many compilation units.

//cube.h
inline double cube(double side)
{
   return side * side * side;
}

//cube.cpp
int main( )
{
    cube(5);
}

Defining it regularly is the normal method, where it's (usually) defined in a cpp file, and linked against. It is not easily used from other compilation units.

//cube.cpp
double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

A prototype allows you to tell the compiler that a function will exist at link time, even if it doesn't exist quite yet. This allows main to call the function, even though it doesn't exist yet. Commonly, prototypes are in headers, so other compilation units can call the function, without defining it themselves. This has the fastest compilation time, and the function is easily used from other compilation units.

//cube.h
double cube(double);

//cube.cpp
int main( )
{
    cube(5);
}

double cube(double side)
{
   return side * side * side;
}
一桥轻雨一伞开 2024-12-19 10:42:25

性能方面,它们都是相同的,因为 inline 只是编译器的提示。如果使用声明/定义分离并且定义位于不同的翻译单元上,那么编译器将更难内联它(但有这样做的实现)。

使函数内联或不内联的区别在于,如果链接器多次看到函数的相同内联定义,则它不会抱怨。

Performance wise, they are all the same since inline is just a hint for the compiler. If the separation of declaration/definition is used and the definition is on a different translation unit, then it will be harder for the compiler to inline it (but there are implementations that do so).

A difference with making a function inline or not is that the linker will not complain if it sees the same inline definition for a function more than once.

压抑⊿情绪 2024-12-19 10:42:25

这3个程序编译后与g++ -S -O3 $file.cc完全相同。
除了第二个示例之外,尽管在 int main() 中内联,但 doublecube(double side) 的定义仍然以非内联形式存在。

_main:
pushl   %ebp
movl    $16, %eax
movl    %esp, %ebp
subl    $8, %esp
andl    $-16, %esp
call    __alloca
call    ___main
leave
xorl    %eax, %eax
ret

The 3 program compile to exactly the same with g++ -S -O3 $file.cc.
Except for the second example where the definition of double cube(double side) still exist in a non inlined form though inlined in int main().

_main:
pushl   %ebp
movl    $16, %eax
movl    %esp, %ebp
subl    $8, %esp
andl    $-16, %esp
call    __alloca
call    ___main
leave
xorl    %eax, %eax
ret
少钕鈤記 2024-12-19 10:42:25

当您声明内联函数时,编译器会尝试通过或多或少地将函数体复制到调用它的位置来加速代码。这只是一个建议,由编译器决定是否可能。

我不确定第三个示例中会发生什么。我想这取决于所使用的工具链。

When you declare a function inline the compiler tries to speed up the code by more or less copying the body of the function to where it's called. It's only a suggestion and it's up the the compiler to make the decision on if it's possible.

I'm not sure what would happen in the 3rd example. I imagine it would depend on the tool chain being used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文