内联函数的使用

mz126 2015-06-19 05:52:00
看书看到内联函数讲内联函数并不是真的发生函数调用,而是把内联函数代码嵌入到调用点处。所以写了一段代码测试一下。但是却发现程序最后的打印语句并没有打印将printf函数语句嵌入到main函数中的位置,而就是他本身的位置。这不就说明他没有发生嵌入吗?求大神指导

#include <cstdio>
#include <cstdlib>

inline void print()
{
printf( "%s %d %s\n" , __FILE__ , __LINE__ , __FUNCTION__ );
}

int main( void )
{
print();

return 0;
}
...全文
129 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
brookmill 2015-06-20
  • 打赏
  • 举报
回复
用Visual Studio的命令行,输入 cl /P test.c 这个命令会生成一个test.i文件,内容如下 #line 1 "test.c" inline void print() { printf( "%s %d %s\n" , "test.c" , 6 , __FUNCTION__ ); } int main( void ) { print(); return 0; }
brookmill 2015-06-20
  • 打赏
  • 举报
回复
编译的第一步是预处理,其中一个任务就是宏替换,把__FILE__之类的就都替换掉了。内联的时候__LINE__已经被替换掉了,怎么联都是个常数 编译器看到的代码是这样的: $ gcc -E test.c # 1 "test.c" # 1 "<command-line>" # 1 "test.c" inline void print() { printf( "%s %d %s\n" , "test.c" , 6 , __FUNCTION__ ); } int main( void ) { print(); return 0; }
赵4老师 2015-06-19
  • 打赏
  • 举报
回复
inline Specifier The inline specifier instructs the compiler to replace function calls with the code of the function body. This substitution is “inline expansion” (sometimes called “inlining”). Inline expansion alleviates the function-call overhead at the potential cost of larger code size. The inline keyword tells the compiler that inline expansion is preferred. However, the compiler can create a separate instance of the function (instantiate) and create standard calling linkages instead of inserting the code inline. Two cases where this can happen are: Recursive functions. Functions that are referred to through a pointer elsewhere in the translation unit. Note that for a function to be considered as a candidate for inlining, it must use the new-style function definition. Functions that are declared as inline and that are not class member functions have internal linkage unless otherwise specified. Microsoft Specific The __inline keyword is equivalent to inline. The __forceinline keyword instructs the compiler to inline the function without performing any cost/benefit analysis. The programmer must exercise good judgement in using this keyword. Indiscriminate use of __forceinline can result in larger, and sometimes even slower, code. Even with __forceinline, the compiler cannot inline code in all circumstances. The compiler cannot inline a function if: The function or its caller is compiled with /Ob0 (the default option for debug builds). The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other). The function has a variable argument list. The function uses inline assembly, unless compiled with /Og, /Ox, /O1, or /O2. The function returns an unwindable object by value, when compiled with /GX, /EHs, or /EHa. The function receives a copy-constructed object passed by value, when compiled with /-GX, /EHs, or /EHa. The function is recursive and not accompanied by #pragma(inline_recursion, on). With the inline_recursion pragma, recursive functions can be inlined to a depth of eight calls, or as determined by the inline_depth pragma (see below). The function is virtual. The program takes the address of the function. If the compiler cannot inline a function declared with __forceinline, it generates a level 1 warning (4714). END Microsoft Specific As with normal functions, there is no defined order of evaluation of the arguments to an inline function. In fact, it could be different from the order in which the arguments are evaluated when passed using normal function call protocol. Microsoft Specific Recursive functions can be substituted inline to a depth specified by theinline_depth pragma. After that depth, recursive function calls are treated as calls to an instance of the function. Theinline_recursion pragma controls the inline expansion of a function currently under expansion. See theInline-Function Expansion (/Ob) compiler option for related information. END Microsoft Specific

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧