A
Asbjørn Sæbø
I am replacing a number of functions with macros. Some of these
functions "do something" and then return a variable. This behaviour I
have found that I can emulate using the comma operator:
#define MYMACRO (do_something(), file_scope_variable - get_a_value() )
Other functions need local storage (but do not return a value), and
can be emulated using a block (a compund statement) to provide a scope
for a variable:
#define OTHERMACRO(some_var) \
{ \
uint8_t k; \
get_some_value(&k); \
some_var += k; \
}
The problem I have run into is a to replace a function that does both
of these, i.e. it both needs to allocate local storage and returns a
value. This seems to be problematic. I think (as far as I have found
in the standard) that a statement (e.g. a compund statement) does not
have (yield) a value. An introducing a block into a comma operator
expression gives compilation errors.
The C FAQ lists a number of techniques, but none of those seem to do
what I want here.) Is there a way to write a macro that can replace
such a function, or am I correct in assuming that it is not possible?
Asbjørn Sæbø
functions "do something" and then return a variable. This behaviour I
have found that I can emulate using the comma operator:
#define MYMACRO (do_something(), file_scope_variable - get_a_value() )
Other functions need local storage (but do not return a value), and
can be emulated using a block (a compund statement) to provide a scope
for a variable:
#define OTHERMACRO(some_var) \
{ \
uint8_t k; \
get_some_value(&k); \
some_var += k; \
}
The problem I have run into is a to replace a function that does both
of these, i.e. it both needs to allocate local storage and returns a
value. This seems to be problematic. I think (as far as I have found
in the standard) that a statement (e.g. a compund statement) does not
have (yield) a value. An introducing a block into a comma operator
expression gives compilation errors.
The C FAQ lists a number of techniques, but none of those seem to do
what I want here.) Is there a way to write a macro that can replace
such a function, or am I correct in assuming that it is not possible?
Asbjørn Sæbø