inline函数问题!

NesTa_xP 2008-02-19 05:20:14

void a(){/*...*/}
void b(){/*...*/}
inline void c()
{a();
b();
}


此时函数c是inline函数,在c里面调用的函数a和函数b会不会自动设置为inline函数啊??
...全文
126 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fish6344 2008-02-19
  • 打赏
  • 举报
回复
void a(){/*...*/}
void b(){/*...*/}
inline void c()
{
a();
b();
}

上述c函数如我们所见,程序员指定其为inline,但这只是对编译器地一种建议而以。如果其中的a和b符合内联的要求(不同的编译器自有一种算法确定),即使没有inlien关键字修饰,编译器也可能为其实现内联的。

综上所述,c及其中a和b函数是否被内联,视编译器自定的内联要求而定。而我们希望被内联的以及没有有意希望被内联的函数,终是否被编译器实现为inline,具有不确定性。引用Stanley B.Lippman的一句话:"...通常你必须进入汇编器中才能看到是否真的实现了inline"!(见《深度探索C++对象模型》page185)
NesTa_xP 2008-02-19
  • 打赏
  • 举报
回复
晕.我不懂啊 到底是怎么一回事??
visame 2008-02-19
  • 打赏
  • 举报
回复
再看下面这两句:

Inline code will not be generated when using a variable number of arguments to a function call (e.g. printf would not be inlined) or a variable sized data type, e.g.

void foo(int n) {
char str[n];
...
}
Compilers have a limit to the depth they will inline functions, e.g. if inline A calls inline B, which calls inline C and so on. This is often configurable through a pragma or command line switch.

再看这个更牛的,似乎是在说明你的想法是正确的,a(),b()的确是被inline了。

There's an example of a function calling another function and how inlining may save you copying the parameters when you pass them to a function and copying the result it returns and stuff. There's also a disclaimer saying that it's just an example and many different things can happen.

visame 2008-02-19
  • 打赏
  • 举报
回复
MARK!
你是说下面这个吗:

/*
The drawback is that fibon_elem() now requires three function calls to complete its operation, whereas previously it required only one. Is this additional overhead critical? That depends on the context of its use. If its performance should prove unacceptable, one solution is to fold the three functions back into one. In C++, an alternative solution is to declare the functions as inline.
*one solution is to fold the three functions back into one.
*an alternative solution is to declare the functions as inline.
根据以上分析,似乎a();b();都是自动变成inline了。你是正确的。否则就不能把three functions back into one。
*/
bool fibon_elem( int pos, int &elem )
{
const vector<int> *pseq = fibon_seq( pos );

if ( ! pseq )
{ elem = 0; return false; }

elem = (*pseq)[ pos-1 ];
return true;
}
// ok: now fibon_elem() is an inline function
inline bool fibon_elem( int pos, int &elem )
{ /* definition same as above */ }

NesTa_xP 2008-02-19
  • 打赏
  • 举报
回复
essential c++
page56
里面就是为什么给fibon_elem函数加了inline,为什么不给他里面调用的其他函数加inline呢??
ttkk_2007 2008-02-19
  • 打赏
  • 举报
回复
不会
hy_number_one 2008-02-19
  • 打赏
  • 举报
回复
不会!!
csdn5211 2008-02-19
  • 打赏
  • 举报
回复
不会,依然会用栈来调用。

64,850

社区成员

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

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