Re: alternate function names

From: Derk Gwen (derkgwen_at_HotPOP.com)
Date: 12/10/03


Date: Wed, 10 Dec 2003 01:44:15 -0000


# What I have been doing with this is giving the macro the interface name and
# naming the function slightly different. I have been putting a "_" in front
# of the function name, which I want to get away from doing. So I have a macro
# like:

You can have a function name the same as a #define with arguments. Parenthesise
the function name but not the #define:

#define xyzzy(parrot) (xyzzy)(parrot-2,parrot+2)
int (xyzzy)(int plugh,int plover);
int (xyzzy)(int plugh,int plover) {
        return plugh*plover;
}

int main(int argc,char **argv) {
        printf("%d\n",(xyzzy)(4,8));
        printf("%d\n",xyzzy(6));
        return 0;
}

# #define strjoin(a...) (_strjoin(a,((const char*)(NULL))))
# and a function definition like:
# char *_strjoin(const char *s,...)
#
# The alternative I'm looking at is to just use the same name for both macro
# and function, like:
# #define strjoin(a...) ((strjoin)(a,((const char*)(NULL))))
# and:
# char *(strjoin)(const char *s,...)
#
# This approach works, but I'm worried about confusion it may cause. So what I
# want to do here is see if there is a better alternative to using a different

As long as you use #defines with arguments and parenthesise the function name,
there will be no confusion in the compiler. (If you look at some of your
implementation standard headers, you'll see them doing this trick.) Whether
it confuses other programmers is a different issue.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
This is one wacky game show.


Relevant Pages

  • Re: some advice
    ... It seems too I copy most of these macro ... I commenti su line che iniziano (dopo eventuali ... char *nome_file; ... int catstr ...
    (alt.lang.asm)
  • Re: A Problem With GD
    ... What does a compiler know? ... long long int are often larger than int. ... It leads to a MACRO! ... void, signed char, unsigned char, char, short, ...
    (comp.lang.perl.misc)
  • Re: variadic macros and and empty replacement for __VA_ARGS__
    ... char *s; ... DBG("message with an int argument, value is %d\n", i); ... This basically used a *function* instead of a macro. ... useful for filtering/enabling/disabling runtime logging for different ...
    (comp.lang.c)
  • Re: A sample RosAsm macro (actually a whole set)
    ... Macro in C are dangerous because they are free to make disasters ... #define UC unsigned char ... void free_node ... int undef ...
    (alt.lang.asm)
  • Re: alternate function names
    ... So I have a macro ... | int (int plugh,int plover) { ... Things like this could be the confusion I want to avoid. ...
    (comp.lang.c)