Re: Coding style question

From: Alex Rousskov <rousskov_at_measurement-factory.com>
Date: Tue, 21 Aug 2012 08:47:02 -0600

On 08/21/2012 12:19 AM, Kinkie wrote:
>> .h file:
>> > #if OPTIONAL_FEATURE
>> > extern void someFunction();
>> > #else
>> > // #define someFunction() // NOP
>> > // or:
>> > // static inline someFunction() {/* NOP */}
>> >
>> > #endif
>> >
>> > .cc file:
>> > #if OPTIONAL_FEATURE
>> > void someFunction()
>> > {
>> > //code
>> > }
>> > #endif
>> >
>> > client code:
>> > someFunction(arg);
> If any, I'd go for static inline (or just inline, after all the
> namespace is already polluted).
> Is the cost of the extra function call worth the decreased readibility?

No statics or #defines in headers unless really necessary, please.

Use inline. Inline has the same effect on the namespace as static but
the call to an [empty] inline function is more likely to be removed by
the compiler. If done right, there will be no runtime cost in most cases.

Please be careful about function arguments though. They or related
conversion operators are sometimes unavailable outside of
OPTIONAL_FEATURE protection.

You may have to create a shadow hierarchy of types and names to use this
approach in some cases (some recent Squid code does things like that;
see src/ipc/AtomicWord.h, for example).

This NullObject-like approach works best for commonly used things. If we
are talking about a single obscure function call which requires
#inclusion of a complex header, then it may be best to leave it (and its
#include) inside #ifdef guards to minimize complications.

HTH,

Alex.
Received on Tue Aug 21 2012 - 14:47:06 MDT

This archive was generated by hypermail 2.2.0 : Tue Aug 21 2012 - 12:00:06 MDT