type of an expression

F

fctk

hello

is it possible to determine the type of an expression? something like
operator sizeof but for types...

for example:

typeof(5.0) --> returns "double"
typeof(5+7) --> returns "int"

etc.
 
R

Richard G. Riley

hello

is it possible to determine the type of an expression? something like
operator sizeof but for types...

for example:

typeof(5.0) --> returns "double"
typeof(5+7) --> returns "int"

etc.

Since, taking a guess, there is no evidence of any type information
being stored with the object data then no.

The type information is "stored" in the handles (variables) used to
access that data : and can therefore be lost through casts,

Will be interested to hear anything different as I've never seen or
used runtime type checks in C.
 
J

Jordan Abel

Since, taking a guess, there is no evidence of any type information
being stored with the object data then no.

Yeah, but he's referring to the type of an expression - this is known at
compile time. The type of *(int *)&f is int, and no undefined behavior
would be invoked just like none is invoked currently when passing that
expression to the sizeof operator.
The type information is "stored" in the handles (variables) used to
access that data : and can therefore be lost through casts,

Will be interested to hear anything different as I've never seen or
used runtime type checks in C.

He's not talking about runtime type checks - the typeof operator works
on expressions, not blobs of memory, and is a common extension.
 
C

Chris Smith

is it possible to determine the type of an expression? something like
operator sizeof but for types...

for example:

typeof(5.0) --> returns "double"
typeof(5+7) --> returns "int"

There is no such language feature. However, it's important to note that
types, by definition, are ALWAYS statically determinable at compile
time, so there's never strictly a need to determine the type using an
operator. You just look at the types of the operands, and apply the
language rules to determine the resulting type.

Granted, though, those rules can be confusing, especially in Java 1.5.
I'm not sure it'd help to have the operator then, though, as Java 1.5
has various types that aren't even expressable in language syntax.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Jordan Abel

There is no such language feature. However, it's important to note that
types, by definition, are ALWAYS statically determinable at compile
time, so there's never strictly a need to determine the type using an
operator. You just look at the types of the operands, and apply the
language rules to determine the resulting type.

Yeah, _you_ can look at it, but you can't make the compiler look at it.

#define assign_from_va(v,list) ((v)=va_next((list),typeof(v)))

#define swap(a,b) do { typeof(b) x=(a); (a)=(b); (b)=(x); } while(0)

#define member_bytes(a,b) ((unsigned char *)(&(a))+\
offsetof(typeof(a),b))
Granted, though, those rules can be confusing, especially in Java 1.5.
I'm not sure it'd help to have the operator then, though, as Java 1.5
has various types that aren't even expressable in language syntax.

He doesn't want the type as a string, he wants it as something he can
use as a type to declare other variables with, or cast to, or maybe pass
to offsetof, etc.
 
J

Jordan Abel

Chris Smith said:
[Stuff about Java]

Sorry, wrong newsgroup!

I thought you were talking about Java by analogy, since types _aren't_
always statically determinable in Java itself [unless you want to be
stuck thinking of something as an Object.]
 
R

Richard G. Riley

Yeah, but he's referring to the type of an expression - this is known at
compile time. The type of *(int *)&f is int, and no undefined behavior
would be invoked just like none is invoked currently when passing that
expression to the sizeof operator.


He's not talking about runtime type checks - the typeof operator works
on expressions, not blobs of memory, and is a common extension.

There was nothing to say he was referring to compile time only was
there ? That is clear since the handles have that type information -
its why types and implicit conversions exist - as you pointed out.
 
J

Jordan Abel

There was nothing to say he was referring to compile time only was
there ? That is clear since the handles have that type information -
its why types and implicit conversions exist - as you pointed out.

An expression is a compile-time construct, though.
 
S

santosh

fctk said:
hello

is it possible to determine the type of an expression? something like
operator sizeof but for types...

for example:

typeof(5.0) --> returns "double"
typeof(5+7) --> returns "int"

No. Though you can figure out the type of an expression by carefully
applying the rules of C, there is no provision in the language or the
library to determine the type of an object. I suppose you mean
something analogous to RTTI in C++, but there is no such thing in C.
 
L

lostpencil

fctk 写é“:
hello

is it possible to determine the type of an expression? something like
operator sizeof but for types...

for example:

typeof(5.0) --> returns "double"
typeof(5+7) --> returns "int"

etc.

hi,in the book named exceptional c++ has the code you needed,because
they are not simple ,i can't write all of them here .you can check the
book for help.

My best!
threes
 
J

Jack Klein

fctk ???


hi,in the book named exceptional c++ has the code you needed,because
they are not simple ,i can't write all of them here .you can check the
book for help.

But since this is comp.lang.c, your answer is completely meaningless,
and the book is not relevant at all.
 
J

Jordan Abel

(Ignoring issues with variably modified types.)

It's still not an "expression" anymore at runtime, it's just code like
any other code. _most_ expressions are evaluated at runtime, not just
those involving variably modified types, but an expression is
a syntactic construct, not a functional one.
 

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

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top