portable typeof macro

P

pete

Keith said:
Jack Klein said:
No, there is no such operator.


There is no such thing in C, nor is there any need for such a thing in
C.
[...]

Obviously there's no absolute need for it, since we've gotten along
for many years without it. But it could be useful in some contexts.
For example, you could use it to write a type-generic swap macro,
something like (untested code follows):

#define SWAP(a, b) do { \
typeof(a) tmp = a; b = a; a = tmp; \
} while(0)

I write that, this way:

#define SWAP(A, B, T) \
((void)(*(T) = *(A), *(A) = *(B), *(B) = *(T)))

I think it's bad style to define objects inside of a macro.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top