0

For example, if we have function A, is it possible to tell the compiler that hey you need to inline this function at this point of the code but not do it (make a call to it) at that point.

pythonic
  • 20,589
  • 43
  • 136
  • 219

5 Answers5

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

gcc has attributes noinline and always_inline.

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

ouah
  • 142,963
  • 15
  • 272
  • 331
0

I think the requirement is not useful, and the answer to your question is: No.

However, you can achieve something to that effect by using a macro:

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

void f() {
  f_inline;
}

You can now use f_inline; if you want to force the code of f to be applied in-line.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
0

It doesn't particularly matter whether you inline function calls at all. See What does __inline__ mean ?. I would just write the function non-inlined, and let the compiler decide how to inline it optimally.

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200
0

If the compiler unconditionally honors your use or non-use of the inline keyword, or if you use the gcc extensions __attribute__((__always_inline__)) and __attribute__(__noinline__)), then you can achieve what you want with a simple wrapper function:

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

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

I've written it with the inline keyword, but since compilers will normally treat it just as a hint or even ignore it, you probably want the gcc attribute version.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711