现代C ++编译器可以在cpp文件中定义内联函数 [英] Can modern C++ compilers inline functions that are defined in a cpp file

查看:293
本文介绍了现代C ++编译器可以在cpp文件中定义内联函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道关键字 inline 具有有用的属性。用于将模板特殊化保留在头文件中。
另一方面,我经常读到 inline 几乎没有用作编译器实际内联函数的提示。
此外,关键字不能在cpp文件中使用,因为编译器只要在调用 inline 关键字时检查标记的函数。

I am aware that the keyword inline has useful properties e.g. for keeping template specializations inside a header file. On the other hand I have often read that inline is almost useless as hint for the compiler to actually inline functions. Further the keyword cannot be used inside a cpp file since the compiler wants to inspect functions marked with the inline keyword whenever they are called.

因此,我对现代编译器的自动内联功能(即gcc 4.43)有点困惑。当我在cpp中定义一个函数时,无论如何,如果它认为内联对函数有意义,或者我是否抢夺了一些优化功能,编译器是否可以内联?

Hence I am a little confused about the "automatic" inlining capabilities of modern compilers (namely gcc 4.43). When I define a function inside a cpp, can the compiler inline it anyway if it deems that inlining makes sense for the function or do I rob him of some optimization capabilities ? (Which would be fine for the majority of functions, but important to know for small ones called very often)

推荐答案

这取决于具体的功能(这对于大多数功能来说都是很好的,你的编译标志。使用 -combine -fwhole-program gcc do函数内联跨cpp边界。如果你编译成多个目标文件,我不知道链接器会做多少。

This depends on your compilation flags. With -combine and -fwhole-program, gcc will do function inlining across cpp boundaries. I'm not sure how much the linker will do if you compile into multiple object files.

这篇关于现代C ++编译器可以在cpp文件中定义内联函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆