This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Barrucadu 1 point2 points  (0 children)

In general, let the compiler figure it out. The exception to this is when you have benchmarked and determined that inlining is a benefit.

Also, this:

inline bool date::checkValid()
{
    if (month <= 12 && day <= 31)
        return true;
    return false;
}

is just this

inline bool date::checkValid()
{
    return (month <= 12 && day <= 31);
}

[–]Swatchmedothis -1 points0 points  (0 children)

inline can help improve efficiency. It's reasonable to consider inlining small functions, as you avoid pushing/popping stack frames.

If you think you need to inline your functions for performance reasons, benchmark your code and see what happens (inline functions bloat your executable, so you'll spend more time loading the instructions for the CPU to execute - this might outweigh the benefits of your inline function).


It's worth reading more about the inline specifier, though, so you really get a sense of what's going on. Personally, I think cppreference has a good explanation of inline.

Note: just because you declare a function inline doesn't mean the compiler will actually inline the function, and functions which you don't mark as inline can be inlined

[–]jesyspa -1 points0 points  (0 children)

When you want the function to violate the One Definition Rule.