473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Macros within function-like macros?

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
13 2033
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
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
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
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
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
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
Sandeep a écrit :
can we have an inline function in Standard C ?


In C99, yes.

--
A+

Emmanuel Delahaye
Dec 26 '05 #8
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

"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
"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions...
3
by: Andrew Boothman | last post by:
Hi, This may be the dumbest question ever, but in the following code (using PHP 4.3.8) how do I call b() from within a()? class test { function a() { print "a called"; b();
2
by: Chris Haynes | last post by:
Hello all, I have a structure: typedef struct UVstruct { float u, v; } uv; Inside a function (A) i declare a pointer to an instance of this structure:
4
by: Ralph Noble | last post by:
Does anyone know of a string function in Access that will allow me to count the number of instances one string occurs within another? Or if there is some sort of word count function? If there is,...
9
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR |...
7
by: -Lost | last post by:
I am calling setTimeout within the context of an object, and whilst this exists, it refuses to be passed along to the function I call. For example: $elemById('id').change = function() { // the...
15
by: Mikhail Kovalev | last post by:
Hi all, I have a file which is to be included in another script and which takes several seconds to load(!), and which is actually not always used by the script, so instead of using include or...
5
by: herenbdy | last post by:
I've been working on a Java based game in order to learn Java, but the game is functional only within my IDE (Eclipse). This image shows the file structure of my project:...
9
by: Erwin Moller | last post by:
Hi all, Is it possible (PHP5.2) to find the name of a variable used in the caller of a function from within the function itself? Or to be more clear: ...php code.. $result = foo($abc);...
7
by: vunet | last post by:
I am still not clear about how to reference an object within another object to pass first object to a function: var Parent = { myFunc : function(){ alert("Parent = "+this) }, Child : { //how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.