By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,360 Members | 2,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,360 IT Pros & Developers. It's quick & easy.

Macros within function-like macros?

P: n/a
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

Dec 26 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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.
Dec 26 '05 #2

P: n/a
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.
Dec 26 '05 #3

P: n/a
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
es*****@acm-dot-org.invalid

Dec 26 '05 #4

P: n/a
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.


Dec 26 '05 #5

P: n/a
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.


Dec 26 '05 #6

P: n/a
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...

Dec 26 '05 #7

P: n/a
Sandeep a écrit :
can we have an inline function in Standard C ?


In C99, yes.

--
A+

Emmanuel Delahaye
Dec 26 '05 #8

P: n/a
Emmanuel Delahaye <em***@YOURBRAnoos.fr> 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) ks***@mib.org <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.
Dec 27 '05 #9

P: n/a

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Emmanuel Delahaye <em***@YOURBRAnoos.fr> 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);

...
}
Dec 27 '05 #10

P: n/a
"pemo" <us***********@gmail.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Emmanuel Delahaye <em***@YOURBRAnoos.fr> 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) ks***@mib.org <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.
Dec 27 '05 #11

P: n/a
In article <do**********@volcano1.grnet.gr>,
Giannis Papadopoulos <ip******@inf.uth.gr> 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!

Dec 27 '05 #12

P: n/a
Kenny McCormack wrote:
In article <do**********@volcano1.grnet.gr>,
Giannis Papadopoulos <ip******@inf.uth.gr> 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" ;)
Dec 27 '05 #13

P: n/a

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"pemo" <us***********@gmail.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Emmanuel Delahaye <em***@YOURBRAnoos.fr> 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.

Dec 27 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.