Macros within function-like macros?

Discussion in 'C Programming' started by Anthony de Almeida Lopes, Dec 26, 2005.

  1. Hello,

    I am wondering why it is not possible to have a function-like macro like
    the following:

    #define __nothread(name) do { \
    #ifdef _PTHREAD_H \
    #warning "name is not a thread safe function" \
    } while (0)

    I get the error "test.h:2:2: '#' is not followed by a macro parameter"

    Actually, the exact goal is to have a prototypes such
    as:

    __nothread int function(void);

    which would generate a warning when compiled with _PTHREAD_H or
    _REENTRANT defined.

    Is this possible?
    I am curious how __attribute__ works internally. Right now I'm looking
    through the gcc-3.3.4 source; so, hopefully I'll find the answer to that.

    Thanks,
    -Anthony
    Anthony de Almeida Lopes, Dec 26, 2005
    #1
    1. Advertising

  2. Anthony de Almeida Lopes wrote:
    > Hello,
    >
    > I am wondering why it is not possible to have a function-like macro like
    > the following:
    >
    > #define __nothread(name) do { \
    > #ifdef _PTHREAD_H \
    > #warning "name is not a thread safe function" \
    > } while (0)


    Why not define an inline function instead?

    inline void __nothread(const char *name)
    {
    ...
    }


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
    August Karlstrom, Dec 26, 2005
    #2
    1. Advertising

  3. Anthony de Almeida Lopes

    Flash Gordon Guest

    August Karlstrom wrote:
    > Anthony de Almeida Lopes wrote:
    >> Hello,
    >> I am wondering why it is not possible to have a function-like
    >> macro like
    >> the following:
    >>
    >> #define __nothread(name) do { \


    Using names starting with an underscore is a really bad idea, many of
    them are reserved and it's generally not worth remembering which ones
    can be used and when. In particular, ALL names starting with two
    underscores are always reserved for any use.

    >> #ifdef _PTHREAD_H \


    You can't have one preprocessor directive inside another, that is why
    you can't do this.

    >> #warning "name is not a thread safe function" \
    >> } while (0)

    >
    > Why not define an inline function instead?


    Possibly the OP is not using a C99 compiler since there are not many of
    them around. inline is not part of the more commonly implemented C89
    standard.

    > inline void __nothread(const char *name)


    Again, avoid identifiers starting with underscores.

    > {
    > ...
    > }

    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Dec 26, 2005
    #3
  4. Anthony de Almeida Lopes

    Eric Sosman Guest

    Anthony de Almeida Lopes wrote:
    > Hello,
    >
    > I am wondering why it is not possible to have a function-like macro like
    > the following:
    >
    > #define __nothread(name) do { \
    > #ifdef _PTHREAD_H \
    > #warning "name is not a thread safe function" \
    > } while (0)
    >
    > I get the error "test.h:2:2: '#' is not followed by a macro parameter"


    Because "The resulting [...] token sequence is not processed
    as a preprocessing directive even if it resembles one [...]"
    (ISO/IEC 9899:199, section 6.10.3.4 paragraph 3). In other words,
    because a macro expansion cannot produce a preprocessing directive.

    It might also be noted that C has no #warning directive. The
    line is legal, but is a "non-directive" (section 6.1, paragraph 1).

    > Actually, the exact goal is to have a prototypes such
    > as:
    >
    > __nothread int function(void);
    >
    > which would generate a warning when compiled with _PTHREAD_H or
    > _REENTRANT defined.
    >
    > Is this possible?


    You could do something like this (but see Flash Gordon's
    response about reserved identifiers):

    #if defined _PTHREAD_H || defined _REENTRANT
    #define __nothread >>> "Not thread-safe!" <<<
    #else
    #define __nothread /* empty */
    #endif
    __nothread int function(void);

    .... but I have a hunch you want something a little different
    from what you've described. My guess is that you don't want an
    error when the declaration is compiled with the undesired macros
    defined, but when code compiled with the unwelcome macros calls
    the declared function. The best I can think of for that problem
    is something along the lines of

    int function(void);
    double trouble(int boil);
    ...
    #if defined _PTHREAD_H || defined _REENTRANT
    #define function >>> "Not thread-safe!" <<<
    #define trouble >>> "Not thread-safe!" <<<
    ...
    #endif

    > I am curious how __attribute__ works internally. Right now I'm looking
    > through the gcc-3.3.4 source; so, hopefully I'll find the answer to that.


    __attribute__ is not part of C. Ask in a gnu forum.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 26, 2005
    #4
  5. Anthony de Almeida Lopes

    Sandeep Guest

    can we have an inline function in Standard C ?

    August Karlstrom wrote:
    > Anthony de Almeida Lopes wrote:
    > > Hello,
    > >
    > > I am wondering why it is not possible to have a function-like macro like
    > > the following:
    > >
    > > #define __nothread(name) do { \
    > > #ifdef _PTHREAD_H \
    > > #warning "name is not a thread safe function" \
    > > } while (0)

    >
    > Why not define an inline function instead?
    >
    > inline void __nothread(const char *name)
    > {
    > ...
    > }
    >
    >
    > August
    >
    > --
    > I am the "ILOVEGNU" signature virus. Just copy me to your
    > signature. This email was infected under the terms of the GNU
    > General Public License.
    Sandeep, Dec 26, 2005
    #5
  6. Sandeep wrote:
    > can we have an inline function in Standard C ?


    Yes, if your compiler supports the ISO C99 standard. And please do not post.

    >
    > August Karlstrom wrote:
    >
    >>Anthony de Almeida Lopes wrote:
    >>
    >>>Hello,
    >>>
    >>> I am wondering why it is not possible to have a function-like macro like
    >>> the following:
    >>>
    >>>#define __nothread(name) do { \
    >>>#ifdef _PTHREAD_H \
    >>>#warning "name is not a thread safe function" \
    >>>} while (0)

    >>
    >>Why not define an inline function instead?
    >>
    >> inline void __nothread(const char *name)
    >> {
    >> ...
    >> }
    >>
    >>
    >>August
    >>
    >>--
    >>I am the "ILOVEGNU" signature virus. Just copy me to your
    >>signature. This email was infected under the terms of the GNU
    >>General Public License.

    >
    >
    Giannis Papadopoulos, Dec 26, 2005
    #6
  7. Giannis Papadopoulos wrote:
    > Sandeep wrote:
    >
    >> can we have an inline function in Standard C ?

    >
    >
    > Yes, if your compiler supports the ISO C99 standard. And please do not
    > post.


    please do not top post... I knew I forgot something...
    Giannis Papadopoulos, Dec 26, 2005
    #7
  8. Sandeep a écrit :
    > can we have an inline function in Standard C ?


    In C99, yes.

    --
    A+

    Emmanuel Delahaye
    Emmanuel Delahaye, Dec 26, 2005
    #8
  9. Emmanuel Delahaye <> writes:
    > Sandeep a écrit :
    >> can we have an inline function in Standard C ?

    >
    > In C99, yes.


    It's also a common extension in pre-C99 compilers.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 27, 2005
    #9
  10. Anthony de Almeida Lopes

    pemo Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Emmanuel Delahaye <> writes:
    >> Sandeep a écrit :
    >>> can we have an inline function in Standard C ?

    >>
    >> In C99, yes.

    >
    > It's also a common extension in pre-C99 compilers.


    But it's still only a 'hint'.

    This 'macro function' swap will *always* be inlined

    #define MXORSwap(x, y) \
    if((x) != (y)) \
    {\
    (*x) ^= (*y);\
    (*y) ^= (*x);\
    (*x) ^= (*y);\
    }

    whereas this 'real function' version may not be, plus it's not 'as generic'
    of course.

    inline void FXORSwap(int * x, int * y)
    {
    if(x != y)
    {
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
    }
    }

    int main(void)
    {
    int a; int b;

    a = 42; b = 24;

    MXORSwap(&a, &b);

    FXORSwap(&a, &b);

    ...
    }
    pemo, Dec 27, 2005
    #10
  11. "pemo" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> Emmanuel Delahaye <> writes:
    >>> Sandeep a écrit :
    >>>> can we have an inline function in Standard C ?
    >>>
    >>> In C99, yes.

    >>
    >> It's also a common extension in pre-C99 compilers.

    >
    > But it's still only a 'hint'.
    >
    > This 'macro function' swap will *always* be inlined
    >
    > #define MXORSwap(x, y) \
    > if((x) != (y)) \
    > {\
    > (*x) ^= (*y);\
    > (*y) ^= (*x);\
    > (*x) ^= (*y);\
    > }
    >
    > whereas this 'real function' version may not be, plus it's not 'as generic'
    > of course.
    >
    > inline void FXORSwap(int * x, int * y)
    > {
    > if(x != y)
    > {
    > *x ^= *y;
    > *y ^= *x;
    > *x ^= *y;
    > }
    > }

    [snip]

    Please read question 10.3 in the C FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 27, 2005
    #11
  12. In article <dopcal$ssk$>,
    Giannis Papadopoulos <> wrote:
    >Sandeep wrote:
    >> can we have an inline function in Standard C ?

    >
    >Yes, if your compiler supports the ISO C99 standard. And please do not post.


    Good advice for one and all!
    Kenny McCormack, Dec 27, 2005
    #12
  13. Kenny McCormack wrote:
    > In article <dopcal$ssk$>,
    > Giannis Papadopoulos <> wrote:
    >
    >>Sandeep wrote:
    >>
    >>>can we have an inline function in Standard C ?

    >>
    >>Yes, if your compiler supports the ISO C99 standard. And please do not post.

    >
    >
    > Good advice for one and all!
    >


    I forgot though the "top".. So it becomes "And please do not top post" ;)
    Giannis Papadopoulos, Dec 27, 2005
    #13
  14. Anthony de Almeida Lopes

    pemo Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "pemo" <> writes:
    >> "Keith Thompson" <> wrote in message
    >> news:...
    >>> Emmanuel Delahaye <> writes:
    >>>> Sandeep a écrit :
    >>>>> can we have an inline function in Standard C ?
    >>>>
    >>>> In C99, yes.
    >>>
    >>> It's also a common extension in pre-C99 compilers.

    >>
    >> But it's still only a 'hint'.
    >>
    >> This 'macro function' swap will *always* be inlined
    >>
    >> #define MXORSwap(x, y) \
    >> if((x) != (y)) \
    >> {\
    >> (*x) ^= (*y);\
    >> (*y) ^= (*x);\
    >> (*x) ^= (*y);\
    >> }
    >>
    >> whereas this 'real function' version may not be, plus it's not 'as
    >> generic'
    >> of course.
    >>
    >> inline void FXORSwap(int * x, int * y)
    >> {
    >> if(x != y)
    >> {
    >> *x ^= *y;
    >> *y ^= *x;
    >> *x ^= *y;
    >> }
    >> }

    > [snip]
    >
    > Please read question 10.3 in the C FAQ, <http://www.c-faq.com/>.


    Yes, I've seen that, and, hmmm, maybe I should have used another example to
    make 'the point' that I was attempting.
    pemo, Dec 27, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    80
    Views:
    2,423
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    444
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    495
  4. Stephen Sprunk

    Help with function-like macros

    Stephen Sprunk, Dec 31, 2004, in forum: C Programming
    Replies:
    4
    Views:
    360
    Tim Rentsch
    Jan 5, 2005
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    470
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page