473,616 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

portable typeof macro

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
20 21733
On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yaho o.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 26 '06 #2
"rkk" <te*******@yaho o.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_Keit h) 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
Jack Klein <ja*******@spam cop.netwrites:
On 25 Dec 2006 21:52:07 -0800, "rkk" <te*******@yaho o.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_Keit h) 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
Keith Thompson a écrit :
"rkk" <te*******@yaho o.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
jacob navia <ja***@jacob.re mcomp.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_Keit h) 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
Keith Thompson wrote:
Jack Klein <ja*******@spam cop.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
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
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
Denis Kasak wrote:
Keith Thompson wrote:
>Jack Klein <ja*******@spam cop.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2010
by: Henri Schomäcker | last post by:
Hi folks, I have a quite big class, which I want to use on UNIX-like systems and on win32. Until now, everything is absolutely portable. But now I need to read a directory and use the os dependent functions. So need to define and include different libraries and functions for both OS-types. How can I do this using e.g. compiler #define's that all compilers understand? And can I do this also inside of a function?
20
1781
by: Matthias | last post by:
Hello, I am missing certain functionality of std::string, so I am currently writing some helper functions which operate on strings. On of them is as follows (it's actually two functions): inline char to_lower ( char c ) { if( c>=65 && c<=90 ) // A-Z return c += 32;
0
2265
by: Arkadiy | last post by:
Hi all, In hope that the following may be of some interest here, I am re-posting my latest anouncement on the Boost mailing list: The typeof submission has been updated to include the latest features, most notably support for *template template parameters*. Please take it in the Boost sandbox file vault, http://boost-sandbox.sourceforge.net/vault/ (typeof.zip).
4
2463
by: aaronfude | last post by:
Hi, Perhaps this is slightly offtopic. Coming into the Windows world from Unix. Is there a reference on writing C++ libraries in a portable way. For example, right now I'm sticking "__declspec(dllexport)" in front of each of my classes. I guess I can still make it work on Unix bydefing
5
1563
by: grid | last post by:
Hi, I recently came across a piece of code like this, ( & ( ( ( some struct * ) 0 ) -> element ) ) ) I had never seen this kind of code being used in any implementations so far, but somehow remembered to have seen it sometime in clc, and that it was for finding the offset of a member in a structure ( if I am not horribly mistaken). Now does this work ? Does this actually use the address 0x0, and when we
131
6103
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write portable C code - *not only* because, in a 'C sense', I
20
6171
by: William Ahern | last post by:
Thoughts, comments? #ifdef HAVE_TYPEOF /* * Can use arbitrary expressions */ #define alignof(t) \ ((sizeof (t) > 1)? offsetof(struct { char c; typeof(t) x; }, x) : 1)
15
29328
by: Key9 | last post by:
Hi all Q1: On reading source code of STL, I just confuse about why c++ not support such a pesudo code ? //pesudo code int a = new int()£» Typeof(a) b = new Typeof(a)()£»
162
6656
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. http://www.developerdotstar.com/community/node/291 This "teacher of C" demonstrates his prowess with a masterful display of incompetence in a 200-line program that travels as swiftly as possible to Segfault City.
0
8203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8146
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5550
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4141
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2579
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.