0

例如,如果我们有函数 A,是否可以告诉编译器你需要在代码的这一点内联该函数,但在该点不这样做(调用它)。

4

5 回答 5

4

You cannot selectively tell a compiler to inline some calls, atleast not portably.

Note that inline is just an suggestion to the compiler, the compiler may or may not obey the suggestion to inline the body of the function inline to the point of call but some conditions like One definition rules will be relaxed by the compiler for such a function.

于 2012-09-19T15:17:33.683 回答
1

gcc有属性noinlinealways_inline

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

于 2012-09-19T15:21:21.640 回答
0

是否内联函数调用并不特别重要。请参阅__inline__ 是什么意思?. 我只会编写非内联函数,让编译器决定如何以最佳方式内联它。

于 2012-09-19T15:21:22.117 回答
0

我认为这个要求没有用,你的问题的答案是:没有。

但是,您可以通过使用宏来实现该效果:

#define f_inline do { int i = 1 + 2; } while( 0 )

void f() {
  f_inline;
}

f_inline;如果您想强制内联应用代码,您现在可以使用f

于 2012-09-19T15:19:58.327 回答
0

如果编译器无条件地尊重您使用或不使用inline关键字,或者如果您使用 gcc 扩展__attribute__((__always_inline__))and __attribute__(__noinline__)),那么您可以使用简单的包装函数来实现您想要的:

static inline int foo_inline(int a, int b)
{
    /* ... */
}

static int foo_noninline(int a, int b)
{
    return foo_inline(a, b);
}

我已经用inline关键字编写了它,但是由于编译器通常会将其视为提示甚至忽略它,因此您可能需要 gcc 属性版本。

于 2012-09-19T17:59:49.933 回答