Context:
I'm writing a module in C which defines some very short functions. My gut instinct would be to declare them static inline
in the .h file, but...
The question:
I understand that GCC at -O2 and above can choose to inline functions, whether or not they're defined static and/or inline. Given that, under what circumstances would you explicitly declare a function static inline
in the .h file?
[FWIW, I'm inclined to trust the compiler...]
To make it concrete (omitting #includes and guards, etc):
Version A: with inline declarations
// File: bvec.h
static inline void bvec_set(size_t bit_index, uint8_t *store) {
store[bit_index >> 3] |= (1 << bit_index & 0x7);
}
static inline void bvec_clear(size_t bit_index, uint8_t *store) {
store[bit_index >> 3] &= ~(1 << bit_index & 0x7);
}
// File: bvec.c
// (empty)
Version B: trust the compiler's -finline-functions
// File: bvec.h
void bvec_set(size_t bit_index, uint8_t *store);
void bvec_clear(size_t bit_index, uint8_t *store);
// File: bvec.c
void bvec_set(size_t bit_index, uint8_t *store) {
store[bit_index >> 3] |= (1 << bit_index & 0x7);
}
void bvec_clear(size_t bit_index, uint8_t *store) {
store[bit_index >> 3] &= ~(1 << bit_index & 0x7);
}