Which is better for short functions (e.g. getters and setters)
inline
keyword or extern inline
?)You can't put inline functions in the source file (and have them used as inline) because their definition won't be available at the points the compiler needs them to inline the code.
Between the other two options, I tend to put one liners into the class definition and any others at the end of the header.
In the class definition header file
typically, unless your build times are more important (assuming that's not the case, based on the question). an exception follows
At the end of the header file
rarely. i have when dealing with nasty templates, just to clean things up. the body is usually so small that it is not distracting, and this can expose implementation details (as a substitute for documentation, but i figure some people will rail me for that). example:
void compute() {
assert(this->hasEnoughEnergyToCompute());
...computing
}
this approach can be good hygiene in some cases. i've actually used secondary files for this (a fourth option).
In the source file
this option is ideal - if it's visible in every translation where you call it. otherwise, send it back to the header.
in this case should I use the inline keyword or extern inline?
just 'inline'. in fact, your compiler probably declares methods inline implicitly. omit it altogether if all your required compilers support this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With