0

Here is a macro that defines a function (I deliberately simplified my macro) :

#define GET_VALUE_IMPL(func_name, value_type, get_func)                 \
    static value_type func_name(const char *token, value_type def_value) \
    {                                                                   \
        value_type  value;                                              \
        assert(token != NULL);                                          \
        value = get_func(token, def_value);                             \
        return value;                                                   \
    }

And then, I'm using this macro like this to define two functions, get_int and get_string:

GET_VALUE_IMPL(get_int, int, external_get_int);
GET_VALUE_IMPL(get_string, char*, external_get_str);

Here is my problem: for get_string, I'd like to pass a function as a fourth parameter to GET_VALUE_IMPL that would be called on value after the assignement value = get_func(token, def_value);

How could I do that with the preprocessor?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Guid
  • 2,137
  • 2
  • 20
  • 33
  • 2
    http://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros – Tony The Lion Mar 14 '13 at 09:27
  • Anyway, I could make the fourth parameter mandatory and pass something void for get_int? – Guid Mar 14 '13 at 09:31
  • @TonyTheLion, even though that question is for C++, the top-voted answer is indeed specific to C. I think it's safe to close this as a duplicate. – Shahbaz Mar 14 '13 at 09:34
  • In reality, my GET_VALUE_IMPL is a much bigger function, and I am sad to duplicate the code juste because I need a little extra call for get_string... – Guid Mar 14 '13 at 09:36
  • you could use 2 small helper methods that cast to void pointers and have the main function get_whatever() use void pointers to call the external functions – msam Mar 14 '13 at 09:57

0 Answers0