Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Functions implemented as macros?

Reply
Thread Tools

Functions implemented as macros?

 
 
Pedro Graca
Guest
Posts: n/a
 
      01-19-2008
I wrote a pair of functions to make a string all uppercase or
lowercase.
The functions call a helper static function to do the conversion
including the address of toupper() or tolower() in the parameters.

My questions is:
Does the Standard allow toupper() or tolower() to be implemented as a
macro?
(Unlike for getc(), my documentation doesn't say they can be
implemented as macros)

To be on the safe side, should I add

#ifdef tolower
/* alternate version */
#else
/* my version */
#endif

-----------------------------------------------

This is the code I have:

#include <ctype.h>
#include <stddef.h>

static char *chg_str(char *destin, char *source, size_t n, int(f)
(int)) {
char *d = destin;
while (--n && *source) {
*destin = f(*source);
++source;
++destin;
}
*destin = 0;
return d;
}

char *lo_str(char *destin, char *source, size_t n) {
if (!source || !destin) return NULL;
if (!n) return destin;
return chg_str(destin, source, n, tolower);
}
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      01-19-2008
Pedro Graca wrote:
> I wrote a pair of functions to make a string all uppercase or
> lowercase.
> The functions call a helper static function to do the conversion
> including the address of toupper() or tolower() in the parameters.
>
> My questions is:
> Does the Standard allow toupper() or tolower() to be implemented as a
> macro?


Yes, that permission applies to all standard library functions, unless
it is specifically stated otherwise for a specific function. I found no
contrary statement in any of the obvious places for toupper() and tolower().

....
> To be on the safe side, should I add
>
> #ifdef tolower
> /* alternate version */
> #else
> /* my version */
> #endif


You don't need to get that complicated. As long as the function name is
not immediately followed by a '(' preprocessing token, it cannot be
treated as the invocation of a function-like macro; it has to be treated
as the name of the function. It will therefore decay into a pointer to
that function in most contexts, including:

> return chg_str(destin, source, n, tolower);


 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      01-19-2008
Pedro Graca wrote:

> I wrote a pair of functions to make a string all uppercase or
> lowercase.
> The functions call a helper static function to do the conversion
> including the address of toupper() or tolower() in the parameters.
>
> My questions is:
> Does the Standard allow toupper() or tolower() to be implemented as a
> macro?

Any function is allowed to be implemented as a macro, provided that it has
the same semantics of the function (except for getc/putc, and except for
sequence points), and that there is also a real function with that name.

> To be on the safe side, should I add
>
> #ifdef tolower
> /* alternate version */
> #else
> /* my version */
> #endif

[snip]
> char *lo_str(char *destin, char *source, size_t n) {
> if (!source || !destin) return NULL;
> if (!n) return destin;
> return chg_str(destin, source, n, tolower);

When the identifier for a function-like macro isn't immediately followed
by the `(` token, it doesn't get replaced. So that tolower stays tolower,
which evaluates to the address of the extern function, regardless of
whether there is a macro with that same name. So you can be safe.
> }




--
Army1987 (Replace "NOSPAM" with "email")
 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      01-19-2008
Army1987 wrote:
> Pedro Graca wrote:
> >
> > My questions is:
> > Does the Standard allow toupper() or tolower() to be implemented as a
> > macro?

> Any function is allowed to be implemented as a macro, provided [...]
> that there is also a real function with that name.


Ah! Thank you
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-19-2008
James Kuyper wrote:
>
> Pedro Graca wrote:


> > My questions is:
> > Does the Standard allow toupper() or tolower()
> > to be implemented as a macro?

>
> Yes, that permission applies to all standard library functions, unless
> it is specifically stated otherwise for a specific function.


Is it specifically stated otherwise
for any standard library functions?

I'm unaware of any exceptions.

--
pete
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      01-20-2008
pete wrote:
> James Kuyper wrote:
>> Pedro Graca wrote:

>
>>> My questions is:
>>> Does the Standard allow toupper() or tolower()
>>> to be implemented as a macro?

>> Yes, that permission applies to all standard library functions, unless
>> it is specifically stated otherwise for a specific function.

>
> Is it specifically stated otherwise
> for any standard library functions?


I was trying to mirror the wording from 7.1.4p1: "Each of the following
statements applies unless explicitly stated otherwise in the detailed
descriptions that follow: ... Any function declared in a header may be
additionally implemented as a function-like macro defined in the header,
....".

> I'm unaware of any exceptions.


There's several cases where a standard library feature is explicitly
described as being only a function-like macro, and not a function.
However, I couldn't find any examples of the reverse case: functions
where implementation as a macro was prohibited. 7.1.4p1 permits
exceptions, but there don't seem to be any.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can VHDL be implemented JTAG TAP controller? Bruce Sam VHDL 9 11-25-2008 02:29 PM
Default functions implemented by compiler sujilc@gmail.com C++ 22 07-03-2006 05:27 PM
Why are the min and max values for a particular numeric type implemented as functions? Vijai Kalyan C++ 1 03-21-2006 02:39 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
SocketPro -- A framework implemented on batching/queue, asynchrony and parallel computaion with online compressing Yuancai \(Charlie\) Ye ASP .Net 0 06-07-2004 02:16 PM



Advertisments