portable typeof macro

R

rkk

Hi,

Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?

gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:

#include <stdio.h>

int
main()
{
int i;
typeof(i) j = 10;
i = j + 1;
printf("%d,%d\n",i,j);
return 0;
}

The above code is just a test to see if the compiler supports typeof
macro/method. But only gcc supports it. I then tried __typeof__ which
is described in ISO standards I hope (I'm not sure btw), but again
supported by gcc and not by other compilers.

How does this typeof macro work? Or is there any equivalent
method/macro which is portable or that can be made portable to work
with all well known compilers.

Thanks in advance.

Best Regards
RKK
 
J

Jack Klein

Hi,

Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?

No, there is no such operator.
gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:

#include <stdio.h>

int
main()
{
int i;
typeof(i) j = 10;
i = j + 1;
printf("%d,%d\n",i,j);
return 0;
}

There is no such thing in C, nor is there any need for such a thing in
C.
The above code is just a test to see if the compiler supports typeof
macro/method. But only gcc supports it. I then tried __typeof__ which
is described in ISO standards I hope (I'm not sure btw), but again
supported by gcc and not by other compilers.

No, there is no __typeof__, or anything equivalent.
How does this typeof macro work? Or is there any equivalent

We have no idea how it works here, because it is not part of the C
language. However, it must be based on information that the compiler
has, and therefore the programmer has -- or can have -- as well.
method/macro which is portable or that can be made portable to work
with all well known compilers.

There is no such thing, but then again there really is no need for
such a thing in C.
Thanks in advance.

You are looking for something that does not exist, so you aren't going
to find it. What you should do is explain the problem you are trying
to solve, that you think you need such a feature would help with. Then
we can suggest ways of solving the problem.
 
K

Keith Thompson

rkk said:
Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?

gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:
[snip]

"typeof" is not a macro; it's a gcc-specific keyword. It doesn't
exist in standard C, and there's no portable way to implement it in C.
 
K

Keith Thompson

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)
 
J

jacob navia

Keith Thompson a écrit :
rkk said:
Is there an equivalent typeof macro/method to determine the type of a
variable in runtime & most importantly that works well with most known
C compilers?

gcc compiler supports typeof() macro, but the same code is not getting
compiled in solaris forte compiler and in microsoft VS 2003 compiler. I
tried something like below:

[snip]

"typeof" is not a macro; it's a gcc-specific keyword. It doesn't
exist in standard C, and there's no portable way to implement it in C.

It is not "gcc specific". Lcc-win32 supports the typeof macro

:)
 
K

Keith Thompson

jacob navia said:
Keith Thompson a écrit : [...]
"typeof" is not a macro; it's a gcc-specific keyword. It doesn't
exist in standard C, and there's no portable way to implement it in C.

It is not "gcc specific". Lcc-win32 supports the typeof macro

Does lcc-win32 really implement "typeof" as a macro? How?

Or was that a joke?
 
D

Denis Kasak

Keith said:
Jack Klein said:
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)

Correct me if I'm wrong, but wouldn't such a macro be limited to only
one appearance per variable scope? Should you use it more than once, you
would also be attempting to define a local variable with the same name
more than once (and possibly a different type also).
 
R

Richard Heathfield

Denis Kasak said:
Keith Thompson wrote:


Correct me if I'm wrong, but wouldn't such a macro be limited to only
one appearance per variable scope? Should you use it more than once, you
would also be attempting to define a local variable with the same name
more than once (and possibly a different type also).

No, that's why it's all wrapped up in a { block scope }. The definition goes
out of scope at the closing brace.
 
D

Denis Kasak

Richard said:
Denis Kasak said:


No, that's why it's all wrapped up in a { block scope }. The definition goes
out of scope at the closing brace.

Right. Silly me. I'll never learn to refrain from posting this late.
 
C

CBFalconer

Denis said:
Keith said:
Jack Klein said:
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)

Correct me if I'm wrong, but wouldn't such a macro be limited to
only one appearance per variable scope? Should you use it more
than once, you would also be attempting to define a local variable
with the same name more than once (and possibly a different type
also).

Consider yourself corrected. The declaration of tmp appears within
the block headed by the 'do' keyword, and is strictly local to that
block. After the closing '}' it no longer exists. That's why
Keith has wrapped it in the do while block.
 
J

jacob navia

Keith Thompson a écrit :
jacob navia said:
Keith Thompson a écrit :
[...]
"typeof" is not a macro; it's a gcc-specific keyword. It doesn't
exist in standard C, and there's no portable way to implement it in C.

It is not "gcc specific". Lcc-win32 supports the typeof macro


Does lcc-win32 really implement "typeof" as a macro? How?



Or was that a joke?

Not as a macro, just as a compiler feature
 
R

Richard Heathfield

jacob navia said:
Keith Thompson a écrit :
[...] Lcc-win32 supports the typeof macro

Does lcc-win32 really implement "typeof" as a macro? How?

Or was that a joke?

Not as a macro, just as a compiler feature

Do you mean that it's not a joke as a macro, but *is* a joke as a compiler
feature?
 
R

rkk

My itention for this question is to design & try something in C which
is close to templates in C++ language. Though void* is sufficient
enough to represent any data type in C, I would like to know if there
is a mechanism or a method in C to know the type of a variable at
runtime. When I googled for the same I came across typeof defined by
gcc. But it is restricted only to gcc and not portable to other
compilers.

But after reading your responses I understand such
operator/method/macro isn't required in C & at runtime a variable can
be manipulated by unsigned char pointers when its size is known, but
its type isn't known.

Thanks for your responses.
 
D

Denis Kasak

CBFalconer said:
Denis said:
Keith said:
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)
Correct me if I'm wrong, but wouldn't such a macro be limited to
only one appearance per variable scope? Should you use it more
than once, you would also be attempting to define a local variable
with the same name more than once (and possibly a different type
also).

Consider yourself corrected. The declaration of tmp appears within
the block headed by the 'do' keyword, and is strictly local to that
block. After the closing '}' it no longer exists. That's why
Keith has wrapped it in the do while block.

Thank you for the correction :) I'm still confused as to how I managed
to overlook that.
 
E

Eric Sosman

Denis said:
Right. Silly me. I'll never learn to refrain from posting this late.

A careful inspection of the macro suggests that the hour
is late for everyone: All it accomplishes, despite the suggestive
name, is to set the second argument equal to the first in an
obfuscated way. Use `typeof(a) tmp = b;' and things would be
a little better.

However, a scope problem remains, even if it isn't exactly
the problem you asked about. For example,

int rmp = 1, smp = 2, tmp = 3;
SWAP(rmp, tmp);

expands (with the corrected macro) to

int rmp = 1, smp = 2, tmp = 3;
do {
int tmp = tmp; tmp = rmp; rmp = tmp;
} while(0);

which is perfectly legal but fails to swap anything at all.
The problem can have noisier manifestations, too:

#define tmp "R:\\ramdisk\\temp"
int whiz = 1, bang = 2;
SWAP(whiz, bang);

The argument for typeof always seems to be "It lets me write
type-generic macros" (and in fact, the example always seems to
be SWAP). But it leaves unsolved the deeper issue of "How do I
avoid identifier aliasing?" Absent a solution to the latter, it
seems to me that such macros are just playthings, not something
that could be used with confidence in "industrial-strength" code.
 
R

Randy Howard

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)

let a =5, b = 10 beforehand...

SWAP(a, b);

So, .... tmp = a, or tmp = 5
then b = a, or b = 5
then a = tmp, or a = 5,

afterward,

a = 5, b = 5

hmmm. very interesting swap operation.
 
K

Keith Thompson

Actually, that's not why I wrapped it a block scope. I just used the
"do { ... } while(0)" idiom because it avoids certain problems with
if/else statements; I didn't even think about the scope of tmp. Of
course it addresses the scope problem as well, demonstrating that good
habits can solve problems you didn't even think of.
A careful inspection of the macro suggests that the hour
is late for everyone: All it accomplishes, despite the suggestive
name, is to set the second argument equal to the first in an
obfuscated way. Use `typeof(a) tmp = b;' and things would be
a little better.
Yup.

However, a scope problem remains, even if it isn't exactly
the problem you asked about. For example,

int rmp = 1, smp = 2, tmp = 3;
SWAP(rmp, tmp);

expands (with the corrected macro) to

int rmp = 1, smp = 2, tmp = 3;
do {
int tmp = tmp; tmp = rmp; rmp = tmp;
} while(0);

which is perfectly legal but fails to swap anything at all.
The problem can have noisier manifestations, too:

#define tmp "R:\\ramdisk\\temp"
int whiz = 1, bang = 2;
SWAP(whiz, bang);

The argument for typeof always seems to be "It lets me write
type-generic macros" (and in fact, the example always seems to
be SWAP). But it leaves unsolved the deeper issue of "How do I
avoid identifier aliasing?" Absent a solution to the latter, it
seems to me that such macros are just playthings, not something
that could be used with confidence in "industrial-strength" code.

I think that can be adequately (though not perfectly) addressed by
using a unique name (this time I tested it):

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

Keith Thompson

Randy Howard said:
let a =5, b = 10 beforehand...

SWAP(a, b);

So, .... tmp = a, or tmp = 5
then b = a, or b = 5
then a = tmp, or a = 5,

afterward,

a = 5, b = 5

hmmm. very interesting swap operation.

You see, I *told* you it was untested! :cool:}
 
C

Chris Torek

My itention for this question is to design & try something in C which
is close to templates in C++ language.

Depending on what you mean by "close", this could be anywhere from
trivial to impossible. :)

C++ templates actually provide a compile-time programming language.
This is not really available in C (although as "ld+", whose name
I forget how to spell and am thus not going to attempt, has shown,
you can come sort of close with C99's variadic preprocessor macros).
See <http://homepage.mac.com/sigfpe/Computing/peano.html> for
instance.
 
S

Stephen Sprunk

jacob navia said:
Keith Thompson a écrit :

It is not "gcc specific". Lcc-win32 supports the typeof macro

:)

s/macro/keyword/

So does ICC (at least on Linux). OTOH, the Intel compiler folks have
made a very determined effort to be a drop-in replacement for GCC and
MSVC in order to gain marketshare.

It's not surprising other compilers don't have it. Once you give
programmers typeof(), they're going to start expecting RTTI like in C++.
People who want C++ should use that, not try to turn C into C++ (again).

S
 

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
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top