How to write 'global' inline functions in Objective-C (using C syntax) -
assuming i'm including header file in precompiled header includes bunch of inline functions used helpers wherever needed in of project's tus -- correct way write inlines?
1) static inlines? e.g.:
static inline bool dosomethingwith(foo *bar) { // ... } 2) extern inlines? e.g.:
in shared.h
extern inline bool dosomethingwith(foo *bar); in shared.m
inline bool dosomethingwith(foo *bar) { // ... } my intention inlines to:
- make code less verbose encapsulating common instructions
- to centralize code contain aid future maintenance
- to use them instead of macros sake of type safety
- to able have return values
so far have seen variant 1) in wild. have read (sadly can't find anymore) variant 1) not accurately move inline function's body callers rather creates new function, , extern inline ensures kind of behavior.
make code less verbose encapsulating common instructions
non-inline functions well...
to centralize code contain aid future maintenance
then should have non-inline functions, don't think?
to use them instead of macros sake of type safety
to able have return values
those seem ok me.
well, when write inline functions, make them static - that's typically how it's done. (else can sorts of mysterious linker errors if you're not careful enough.) it's important note inline not affect visibility of function, if want use in multiple files, need static modifier.
an extern inline function not make lot of sense. if have 1 implementation of function, defeats purpose of inline. if use link-time optimization (where cross-file inlining done linker), inline hint for compiler not useful anyway.
only extern inline insures kind of behavior.
it doesn't "ensure" @ all. there's no portable way force inlining - in fact, modern compilers ignore keyword , use heuristics instead decide when inline. in gnu c, can force inlining using __attribute__((always_inline)) attribute, unless have reason that, shouldn't doing it.
Comments
Post a Comment