Questions tagged [inline-functions]

By using keyword 'inline' in function definition, programmer can request that the (C/C++) compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined.

From wikipedia:

In various versions of the C and C++ programming languages, an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. (However, compilers are not obligated to respect this request.)

Typical inline-function in C++:

inline int getLength() const { return _length; }

Typical inline-function in C:

inline int max(int a, int b) 
{ 
    return (a > b) ? a : b;
}

See wiki page for more info.

174 questions
274
votes
14 answers

Benefits of inline functions in C++?

What is the advantages/disadvantages of using inline functions in C++? I see that it only increases performance for the code that the compiler outputs, but with today's optimized compilers, fast CPUs, huge memory etc. (not like in the 1980< where…
Lennie De Villiers
  • 345
  • 4
  • 5
  • 9
166
votes
7 answers

when to use an inline function in Kotlin?

I know that an inline function will maybe improve performance & cause the generated code to grow, but I'm not sure when it is correct to use one. lock(l) { foo() } Instead of creating a function object for the parameter and generating a call, the…
holi-java
  • 29,655
  • 7
  • 72
  • 83
116
votes
7 answers

What does extern inline do?

I understand that inline by itself is a suggestion to the compiler, and at its discretion it may or may not inline the function, and it will also produce linkable object code. I think that static inline does the same (may or may not inline) but will…
wilbur_m
99
votes
9 answers

static variables in an inlined function

I have a function that is declared and defined in a header file. This is a problem all by itself. When that function is not inlined, every translation unit that uses that header gets a copy of the function, and when they are linked together there…
Dirk Groeneveld
  • 2,547
  • 2
  • 22
  • 23
73
votes
7 answers

Python equivalence to inline functions or macros

I just realized that doing x.real*x.real+x.imag*x.imag is three times faster than doing abs(x)**2 where x is a numpy array of complex numbers. For code readability, I could define a function like def abs2(x): return…
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
52
votes
4 answers

Is there a way to declare an inline function in Swift?

I'm very new to the Swift language. I wanted to declare an inline function just like in C++ so my func declaration looks like this: func MyFunction(param: Int) -> Int { ... } and I want to do something like this: inline func MyFunction(param:…
Gibnem
  • 675
  • 1
  • 8
  • 9
45
votes
12 answers

When should I use __forceinline instead of inline?

Visual Studio includes support for __forceinline. The Microsoft Visual Studio 2005 documentation states: The __forceinline keyword overrides the cost/benefit analysis and relies on the judgment of the programmer instead. This raises the…
Michael Labbé
  • 12,017
  • 4
  • 27
  • 36
42
votes
1 answer

What's the difference between static inline, extern inline and a normal inline function?

What's the difference between a static inline, extern inline and a normal inline function? I've seen some vague explanations about this. As far as I've understood, static inline is not just an inline function that is meant to only be referred to…
Forever a noob
  • 689
  • 1
  • 9
  • 16
40
votes
2 answers

Are the "inline" keyword and "inlining" optimization separate concepts?

I am asking this basic question to make the records straight. Have referred this question and its currently accepted answer, which is not convincing. However the second most voted answer gives better insight, but not perfect either. While reading…
iammilind
  • 68,093
  • 33
  • 169
  • 336
39
votes
13 answers

What is wrong with using inline functions?

While it would be very convenient to use inline functions at some situations, Are there any drawbacks with inline functions? Conclusion: Apparently, There is nothing wrong with using inline functions. But it is worth noting the following…
prakash
  • 58,901
  • 25
  • 93
  • 115
33
votes
16 answers

Why should I ever use inline code?

I'm a C/C++ developer, and here are a couple of questions that always baffled me. Is there a big difference between "regular" code and inline code? Which is the main difference? Is inline code simply a "form" of macros? What kind of tradeoff must…
Marcos Bento
  • 2,030
  • 4
  • 22
  • 19
32
votes
9 answers

What Does It Mean For a C++ Function To Be Inline?

See title: what does it mean for a C++ function to be inline?
user7545
  • 3,020
  • 4
  • 22
  • 22
29
votes
3 answers

How to declare an inline function in C99 multi-file project?

I want to define an inline function in a project, compiled with c99. How can I do it? When I declare the function in a header file and give the detail in a .c file, the definition isn't recognized by other files. When I put the explicit function in…
mousomer
  • 2,632
  • 2
  • 24
  • 25
20
votes
4 answers

If inlining is optional, why does removing 'inline' cause linker errors?

I have a class that had an inline member, but I later decided that I wanted to remove the implementation from the headers so I moved the members body of the functions out to a cpp file. At first I just left the inlined signature in the header file…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
20
votes
7 answers

Are member functions always inline, or is it just a hint?

If we define a member function inside the class definition itself, is it necessarily treated inline or is it just a request to the compiler which it can ignore.
shreyasva
  • 13,126
  • 25
  • 78
  • 101
1
2 3
11 12