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

portable typeof macro

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

Dec 26 '06 #1
Share this Question
Share on Google+
20 Replies


P: n/a
On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yahoo.comwrote in
comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 26 '06 #2

P: n/a
"rkk" <te*******@yahoo.comwrites:
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.

--
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 26 '06 #3

P: n/a
Jack Klein <ja*******@spamcop.netwrites:
On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yahoo.comwrote in
comp.lang.c:
>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.
[...]

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)

--
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 26 '06 #4

P: n/a
Keith Thompson a écrit :
"rkk" <te*******@yahoo.comwrites:
>>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

:-)
Dec 27 '06 #5

P: n/a
jacob navia <ja***@jacob.remcomp.frwrites:
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?

--
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 '06 #6

P: n/a
Keith Thompson wrote:
Jack Klein <ja*******@spamcop.netwrites:
>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).

--
Denis Kasak
Dec 28 '06 #7

P: n/a
Denis Kasak said:
Keith Thompson wrote:
<snip>
> #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).
No, that's why it's all wrapped up in a { block scope }. The definition goes
out of scope at the closing brace.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 28 '06 #8

P: n/a
Richard Heathfield wrote:
Denis Kasak said:
>Keith Thompson wrote:

<snip>
>> #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).

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.

--
Denis Kasak

Dec 28 '06 #9

P: n/a
Denis Kasak wrote:
Keith Thompson wrote:
>Jack Klein <ja*******@spamcop.netwrites:
>>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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 28 '06 #10

P: n/a
Keith Thompson a écrit :
jacob navia <ja***@jacob.remcomp.frwrites:
>>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
Dec 28 '06 #11

P: n/a
jacob navia said:
Keith Thompson a écrit :
>jacob navia <ja***@jacob.remcomp.frwrites:
<snip>
>[...] 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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 28 '06 #12

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

Jack Klein wrote:
On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yahoo.comwrote in
comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 28 '06 #13

P: n/a
CBFalconer wrote:
Denis Kasak wrote:
>Keith Thompson wrote:
>>Jack Klein <ja*******@spamcop.netwrites:

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.

--
Denis Kasak

Dec 28 '06 #14

P: n/a
Denis Kasak wrote:
Richard Heathfield wrote:
>Denis Kasak said:
>>Keith Thompson wrote:

<snip>
>>> #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).

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

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 28 '06 #15

P: n/a
On Tue, 26 Dec 2006 17:34:30 -0600, Keith Thompson wrote
(in article <ln************@nuthaus.mib.org>):
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.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 28 '06 #16

P: n/a
Eric Sosman <es*****@acm-dot-org.invalidwrites:
Denis Kasak wrote:
>Richard Heathfield wrote:
>>Denis Kasak said:

Keith Thompson wrote:

<snip>

#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).

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

--
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 29 '06 #17

P: n/a
Randy Howard <ra*********@FOOverizonBAR.netwrites:
On Tue, 26 Dec 2006 17:34:30 -0600, Keith Thompson wrote
(in article <ln************@nuthaus.mib.org>):
>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.
You see, I *told* you it was untested! 8-)}

--
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 29 '06 #18

P: n/a
In article <11**********************@i12g2000cwa.googlegroups .com>
rkk <te*******@yahoo.comwrote:
>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.htmlfor
instance.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jan 1 '07 #19

P: n/a
"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr...
Keith Thompson a écrit :
>"rkk" <te*******@yahoo.comwrites:
>>>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:

"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

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

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Jan 2 '07 #20

P: n/a
Keith Thompson wrote:
>
Jack Klein <ja*******@spamcop.netwrites:
On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yahoo.comwrote in
comp.lang.c:
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.
[...]

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.

--
pete
Jan 3 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.