Built-in and best practices

Possible duplicate:
When to use the built-in function and when not to use it?

I saw a lot of sources using different syntaxes for the inline directive.

 namespace Foo { class Bar { public: // 1 - inline on the declaration + implementation inline int sum1(int a, int b) { return a + b; } // 2 - inline on template declaration + implementation template <typename T> inline T sum2(T a, T b) { return a + b; } // 3 - Nothing special on the declaration... int sum3(int a, int b); }; // 3 - But the inline directive goes after // In the same namespace and still in the header file inline int Bar::sum3(int a, int b) { return a + b; } } 

I could not find the โ€œofficialโ€ recommendations regarding the use of inline : I only know that inline is just a hint to the compiler and that it provides internal binding. I donโ€™t know much.

Here are my questions:

  • Is (1) good practice?
  • Does (2) always need the inline directive? (My guess would be no, but I cannot explain why). When it is necessary?
  • (3) seems to be the most commonly used syntax. Is there something wrong with this or should I use it too?
  • Is there any other inline use (syntax) that I don't know about?
+4
source share
4 answers

No and no! inline is not only a hint to the compiler, but not a forced internal connection.

inline implicit for functions defined in a class, so you only need this for functions defined outside of classes. You should use it when and only when you need to include changes in one definition rule that inline does.

+9
source

Regarding your questions:

  • This does not seem to be a good use, because any member function implemented in the class body is implicitly marked as inline. In other words, this particular inline ad is redundant. However, this function alone is a good candidate for embedding, as it is small, short, and probably uses more build commands to invoke than to execute.

  • The inline keyword is not necessary here because there are two reasons. Firstly, a function is a member function implemented in the body of the class, so it is implicitly built-in. Secondly, this function is a template, and you only need to mark the functions defined in the built-in headers if they are not templates. However, this is actually a good candidate for a built-in function for the above reasons.

  • This is a great candidate for a built-in function, and you make good use of the keyword. This is a short feature that can really benefit from it.

  • No that all syntactic uses. You seem to know what you are doing! :-)

+2
source

There is no need to use the inline keyword, unless you fully implement the free function in the header file. This tells the linker that multiple (duplicated) characters for this function should be allowed to exist, and the linker should reduce this to one instance.

+1
source

By default, if you declare and define a function inside a class, it will be inline. Inline is a request to the compiler, and the compiler decides whether it will be inline or not. In the case of inline, the function body is expanded in the code in which it was called. If the function is large and has several loops and switches, then the compiler ignores the built-in request and treats it as a simple function. A simple way to embed behavior is the same as preprocessors, but the pros and cons are different.

0
source

Source: https://habr.com/ru/post/1335052/


All Articles