Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Compilers can generally move code across translation units so the two should have no difference. Personally I prefer this (all in the header):</p> <pre><code>// math.h class Math { public: static int sum(int a, int b); }; inline int Math::sum(int a, int b) { return a + b; } </code></pre> <p>The reason I prefer this is that there are times when even the best optimizing compilers cannot inline code if you put it in the cpp file, like if the math class was being exported (no inlining across DLL boundaries, e.g.). I don't like to make exceptions to my coding style, so just putting it in the header is guaranteed to <em>allow</em> the compiler to inline it (if it so chooses).</p> <p>The second reason I prefer this explicit style over inlining directly in the class definition is because I work with a lot of lazy coders who often inline things just because it saves time to write instead of actually inlining after a profiling session (which is generally the only time you should consider inlining unless you are a superb compiler/assembly genius). </p> <p>As an important side note, it is quite common to find that inlining makes no noticeable improvement in performance, and there are many cases where not inlining code is better than inlining it. I've found this to even be true of simple one line accessor functions: just because the code, when inlined, requires fewer instructions than it does to call the function doesn't always mean the compiler will do a better job with it. Over-inlining can actually confuse compilers (ex: not using registers very effectively because of too much inlined, exceptional-case code accessing memory). Inlining really should be done with care and the aid of a profiler.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload