0

If I were to declare and define a namespace function function above the main function, as it is shown below, would that result in compiler generating it as an inline function?

I know that when we define a function inside a class declaration, the function is generated as an inline function by the compiler; however, I am not sure if this is the case for the specific case shown below. Would someone be kind enough to clarify the situation for me?

namespace pairs_with_k_diff {
    void inc_and_push_back () {
        // function definition
    }
}

int main() {
// Call this function inside the main later
}
Anil Celik Maral
  • 177
  • 1
  • 12
  • 2
    I'm not sure that "generated as an inline function by the compiler" is a) a precise notion, b) understood correctly by you, and c) something that you can actually "know". The `inline`-ness of a function affects the ODR, and no, your function here is not an inline function, and it would be an ODR violation to include this definition in multiple translation units (unlike for class member functions that are defined inline). Whether separate function code is emitted in the object code is a largely unrelated question. – Kerrek SB Aug 03 '19 at 23:11
  • Could you please explain why class member functions are called inline when they are defined at the same time they are declared and why this doesn't apply to the example that I gave? – Anil Celik Maral Aug 03 '19 at 23:22
  • Define what you mean by "inline". What C++ calls "inline" may not be what you call "inline". –  Aug 03 '19 at 23:26
  • 2
    https://stackoverflow.com/questions/9734175/why-are-class-member-functions-inlined – Paul Sanders Aug 03 '19 at 23:27
  • @Chipster An inline function, as far as I know, is a function which a compiler can replace those function definition wherever those are being called. – Anil Celik Maral Aug 04 '19 at 00:03
  • 1
    You should read https://en.cppreference.com/w/cpp/language/inline - `inline` is not used as it is named. It doesn't actually have much (or anything) to do with whether the compiler actually inlines anything. – Jesper Juhl Aug 04 '19 at 07:17

2 Answers2

2

An inline function, as far as I know, is a function which a compiler can replace those function definition wherever those are being called.

So, let's clear that part up.

Inline has 2 definitions:

  1. It tells the compiler that the function code can be expanded where the function is called, instead of effectively being called.
  2. It tells the compiler that the function definition can be repeated.

You are obviously thinking of definition 1. As the answer goes on to state, that definition is not really used anymore (if it was ever used). inline to the compiler only means that the definition can be repeated. It allows you to put the definition in headers (although it may have other uses too).

So, back you your original question:

If I were to declare and define a namespace function function above the main function, as it is shown below, would that result in compiler generating it as an inline function?

Inline by your definition is only determined by the compiler. You have no control over "inlineness," in this sense of the word, whatsoever.

Of course, if you want the other definition of inline, you have to declare it inline:

namespace pairs_with_k_diff {
    inline void inc_and_push_back () {
        // function definition
    }
}

There is no other way to get this function "inline" (in the second sense of the word) without, of course, declaring it inline. It won't happen automatically.

  • 1
    Thanks a lot for providing the two definitions of inline! I wasn't aware of the second definition especially as it relates to c++. I understand now why my definition was ambiguous. – Anil Celik Maral Aug 04 '19 at 00:24
  • 1
    No problem. That's why I asked. I wanted to make sure we were on the same page, as that affects the answer. –  Aug 04 '19 at 00:28
1

No. Whether a function is defined before or after main function has no effect on whether the function will be inline.

Instead, function can be declared inline by using the keyword inline. Functions that are defined within a class definition, as well as function templates are implicitly inline.

The function pairs_with_k_diff::inc_and_push_back in your example is not an inline function, because inline keyword was not used, and because it is not a member function nor a function template.

as far as I know, is a function which a compiler can replace those function definition wherever those are being called.

That's not quite what it means. See here for more info.

eerorika
  • 232,697
  • 12
  • 197
  • 326