通过函数指针的函数可以被内联? [英] Can functions used through function pointers be inlined?

查看:214
本文介绍了通过函数指针的函数可以被内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdlib.h>

inline int f0(int a) {
  return a*a;
}

inline int f1(int a) {
  return a*a*a;
}

int main() {
  int (*f)(int);
  f = rand()%2 ? f0 : f1;
  return f(rand());
}

所以用gcc,生成的ASM文件是相同的有或无内联。它是相同的任何code函数指针?

So with gcc, asm file generated is same with or without inline. Is it same with any code with function pointers?

推荐答案

函数指针不能被内联,除非它们的值是在编译时完全可判定的。你的情况是不可判定。

Function pointers cannot be inlined unless their value is fully decidable at compile time. Your case is not decidable.

大多数时间函数指针将被内联,即使编译器可以看到哪个功能是在函数指针。

Most of the time function pointers will never be inlined, even if the compiler can see which function is in the function pointer.

这篇关于通过函数指针的函数可以被内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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