Questions tagged [inline]

Use this tag for questions specifically about the effects of the inline keyword, together with the appropriate language tag.

Inline, or inline expansion, is a programming language optimization that inserts the complete body of the function in every place that the function is called. Depending on the programming language, this may be implemented by the compiler, manually or by a keyword.

Historically, in 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, modern compilers usually use their own heuristics and ignore the request. Thus, the inline keyword is now mostly used for its effects on the One Definition Rule.

Inline expansion is used to eliminate the time overhead when a function is called. It is typically used for functions that execute frequently. It also has a space benefit for very small functions, and is an enabling transformation for other optimizations.

3753 questions
738
votes
16 answers

When should I write the keyword 'inline' for a function/method?

When should I write the keyword inline for a function/method in C++? After seeing some answers, some related questions: When should I not write the keyword 'inline' for a function/method in C++? When will the compiler not know when to make a…
Partial
  • 9,529
  • 12
  • 42
  • 57
569
votes
15 answers

How to write inline if statement for print?

I need to print some stuff only when a boolean variable is set to True. So, after looking at this, I tried with a simple example: >>> a = 100 >>> b = True >>> print a if b File "", line 1 print a if b ^ SyntaxError: invalid…
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
301
votes
13 answers

Inline functions in C#?

How do you do "inline functions" in C#? I don't think I understand the concept. Are they like anonymous methods? Like lambda functions? Note: The answers almost entirely deal with the ability to inline functions, i.e. "a manual or compiler…
Dinah
  • 52,922
  • 30
  • 133
  • 149
262
votes
14 answers

When to use the inline function and when not to use it?

I know that inline is a hint or request to the compiler and is used to avoid function call overheads. So, on what basis one can determine whether a function is a candidate for inlining or not? In which case one should avoid inlining?
Ashish
  • 8,441
  • 12
  • 55
  • 92
192
votes
13 answers

Are inline virtual functions really a non-sense?

I got this question when I received a code review comment saying virtual functions need not be inline. I thought inline virtual functions could come in handy in scenarios where functions are called on objects directly. But the counter-argument came…
aJ.
  • 34,624
  • 22
  • 86
  • 128
188
votes
5 answers

How to pass event as argument to an inline event handler in JavaScript?

// this e works document.getElementById("p").oncontextmenu = function(e) { e = e || window.event; var target = e.target || e.srcElement; console.log(target); }; // this e is undefined function doSomething(e) { e = e || window.event; var…
user1643156
  • 4,407
  • 10
  • 36
  • 59
171
votes
7 answers

What's the difference between "static" and "static inline" function?

IMO both make the function to have a scope of the translation unit only. What's the difference between "static" and "static inline" function? Why should inline be put in a header file, not in .c file?
new_perl
  • 7,345
  • 11
  • 42
  • 72
162
votes
9 answers

How can I tell gcc not to inline a function?

Say I have this small function in a source file static void foo() {} and I build an optimized version of my binary yet I don't want this function inlined (for optimization purposes). is there a macro I can add in a source code to prevent the…
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
161
votes
8 answers

Why are C++ inline functions in the header?

NB This is not a question about how to use inline functions or how they work, more why they are done the way they are. The declaration of a class member function does not need to define a function as inline, it is only the actual implementation of…
thecoshman
  • 8,394
  • 8
  • 55
  • 77
154
votes
9 answers

React: inline conditionally pass prop to component

I would like to know if there is a better way to conditionally pass a prop than using an if-statement. For example, right now I have: var parent = React.createClass({ propTypes: { editable: React.PropTypes.bool.isRequired, editableOpts:…
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
149
votes
4 answers

Does it make any sense to use inline keyword with templates?

Since templates are defined within headers and compiler is able to determine if inlining a function is advantageous, does it make any sense? I've heard that modern compilers know better when to inline a function and are ignoring inline hint. edit:…
mip
  • 8,355
  • 6
  • 53
  • 72
146
votes
2 answers

Does constexpr imply inline?

Consider the following inlined function : // Inline specifier version #include #include inline int f(const int x); inline int f(const int x) { return 2*x; } int main(int argc, char* argv[]) { return…
Vincent
  • 57,703
  • 61
  • 205
  • 388
144
votes
8 answers

HTML: Changing colors of specific words in a string of text

I have the below message (slightly changed): "Enter the competition by January 30, 2011 and you could win up to $$$$ — including amazing summer trips!" I currently have:

Mitch
  • 1,441
  • 2
  • 9
  • 3
136
votes
14 answers

Inline functions vs Preprocessor macros

How does an inline function differ from a preprocessor macro?
Subodh
  • 1,393
  • 2
  • 11
  • 8
134
votes
13 answers

Send inline image in email

Having an issue sending an image via email as an embedded image in the body. The image file shows as an attachment which is ok but the inline image portion just shows as a red x. Here is what I have so far LinkedResource inline = new…
Tsukasa
  • 6,342
  • 16
  • 64
  • 96
1
2 3
99 100