Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ typeid()

Name: Anonymous 2006-01-11 12:59

Does anyone know how typeid() is implemented in C++???
I mean, how do I make a function like typeid() where I can pass the TYPE as a paramter.

Example:
int main()
{
    typeid(int);//<-- works of course
    f(int);//<-- how to do something like this
           //        without using macros or templates
}

Name: Anonymous 2006-01-11 13:14

Hack the compiler.

Name: Anonymous 2006-01-11 14:50 (sage)

The typeid operator returns a reference to a type_info object that describes `object`.

Name: Anonymous 2006-01-11 23:09

Yes, I know typeid() returns a reference to a type_info object, but I am wondering how it is implemented in the first place.

Name: Anonymous 2006-01-12 2:57

typeid is an operator, and I don't think you can make a function that takes a keyword as an argument.

Name: Anonymous 2006-01-12 4:56

really? but C++ is suppose to be a very flexible language. I mean, they allow (...), macros, templates, operator overloading etc. I'm SURE there must be a way to make a function that takes a keyword as an argument, right? Maybe not directly of course, since that is syntax error, but there MUST be a way, like meta-template programming for example, where it plays with the syntax to compute during comilation time.

Name: Anonymous 2006-01-12 6:31

>>6
Use Lisp if you want a powerful language.

Name: Anonymous 2006-01-12 6:47 (sage)

>>6
If you refuse to use both macros and templates then you have already tied your hands behind your back.

Name: Anonymous 2006-01-12 6:47

>>6

Flexible != close-to-the-silicon.  And C++ is close to the silicon.  So no, you can't treat types as first-class entities and pass them as parameters like you would anything else to a method.

Of course, you can pass types as template parms.  What are you trying to do in f()?

Name: Anonymous 2006-01-12 11:14

I am sure you can do it. C++ is just a bunch of macros added to C.

Name: Anonymous 2006-01-12 13:03

>>10
That was true in the language's very early days--Cfront was just an elaborate C preprocessor--but modern C++ requires run-time libraries, code generation, and other things.

Name: Anonymous 2006-01-12 17:29

C++ is ultimate now.

Name: Anonymous 2006-01-12 18:46

Hmm...in that case, can anyone provide a macro-template wrapper to simulate a function similar to typeid() to accept types as a parameter?

Example:
#define myFunc(type) myHiddenFunc<type>()

but the above doesn't work like typeid() of course...
because typeid() can evaluate a type during runtime.

Example:
class A{}; class B:A{};
...
B b;
A* pA = &b;
typeid(*pA); // This will evaluate type B
myFunc(*pA); // This will evaluate type A

Any ideas?

Name: Anonymous 2006-01-12 23:52

>>13

In response to "Can anyone provide...": No.

That was easy.  Ask another one.

Types are not first-class entities in C++.  You can't treat them as such.  Attempts to do so in a literal and easy fashion are doomed to failure.

Name: Anonymous 2006-01-13 2:08

>>13
You might want to elaborate on why you would want this and where you would use such a thing. There could be a better way to handle it rather than depending on the type of the variable... without templates.

Name: Anonymous 2006-01-13 4:45

>>15
The main reason why I want to do this, is for learning, actually. Because, in the past I have tried many times in many ways to obtain the true type of a virtual object, similar to what Java has provided. And I am out of luck. With the only possible hint of light being to learn how typeid() is implemented.

>>7
As for the Lisp thing (and any future language posted),
I love C++, hence I plan to do this in C++.
Lisp is ok, but I hate their prefix syntax.

Name: Anonymous 2006-01-13 4:47

Also to clarrify, I intend to get the true typeof an object using only standard C++ techniques (even if they are not common) and do not intend to write a wrapper over all my C++ programs (which is what I am doing now).

Name: Anonymous 2006-01-13 4:51

Hmm...again to clarify,
I do NOT mind using templates and macros anyomore.
So, ANY C++ techniques will be fine.
If it uses the OS, it is fine too...although I prefer not to,
since that would make the program platform dependant...

Name: Anonymous 2006-01-13 6:14

>>16
Enjoy your shitty language that makes you work really hard to get the simplest bit of information.

Name: Anonymous 2006-01-13 6:44

>>1
You can't, because typeid is not a function, but an operator.

>>7
Use LISP if you want an ugly ass syntax and nobody ever reading your code. Use C/C++/C++-- for stuff you're running on your computer right now, including this web browser.

>>19
What information? That information is not there at runtime.

Name: Anonymous 2006-01-13 6:54

>>17

It looks to me as thought you're looking for a way to make an explicit determination of type when types are implicitly decided.    Short of munging around in compiler-specific data structures (v-tables), I don't see a way to do what you want.  But then, I think you're probably approaching it from the wrong angle for this language. 

C++ is most definitely not Java, in spite of their similar syntax.  Don't let the way you did things in Java (e.g., using slow reflection libraries) influence your style in C++ where those facilities just aren't available. 

Instead of trying to turn C++ into Java, learn C++.

This is why >>9 and >>15 are asking *why* you're trying to do this.  They're trying to understand how you might accomplish your goal in a "C++ way".

Name: Anonymous 2006-01-13 8:03

Hi. Thanks for all your constructive comments.
Esspecially >>21
It may be noted that I actually learned C++ first before JAVA or any language for that matter.
Its just that, ever since I learned C++, I've been trying to figure out how to get something like a TYPE-pointer or something, where I can get the true-type of a virtual-object, which typeid() does not provide. And I gave up, until I learnt JAVA and saw that they managed to do what I wanted for so long. So that's why I'm posting this thread here.
I'm SURE there must be a way.
I mean, C++ is a lower-level language compared to JAVA, right?

Name: Anonymous 2006-01-13 8:04

What information? That information is not there at runtime.
EXACTLY

Name: Anonymous 2006-01-13 8:20

>>22
That's a really stupid argument. Just because a language is lower-level it does not mean that it has more powerful introspection. In fact the oposite tends to be true.

Name: Anonymous 2006-01-13 9:15

>>24
Yes yes, I understand what you mean.
But as I stated earlier in >>17
I do not intend to implement high-level wrappers around all my classes, because that is exactly what I am doing now.
Hence, why I said there should be a better way to do it, since C++ is a lower-level language.

Name: pl 2006-01-13 9:26

I wonder whether RTTI (or something similar) didn't allow for this - but don't take that for granted, it has been years since I last used C/C++ (to the extend that I practically don't remember anything).

As for other languages, I remember Delphi (native, not .NET) had some way to create such function. However, you couldn't pass keyword to it (There was something with a sort-of table with values identifying types... I'll have to check it...)

Name: Anonymous 2006-01-13 14:28 (sage)

>>22
If you want to get the friggen type of the object and pass it around like a cheap whore, then use a damn language that allows that already! C++ IS NOT JAVA.

Name: Anonymous 2006-01-13 18:52

>>25

As >>21 said, that information lives in the v-tables.  AFAIK, *only* in the V-tables.  V-tables are not part of the standard language, but rather an implementation detail.  There is no standard or portable way to get what you want.  Period.  End of story.  C++ has no reflection or introspection capabilities.  Period.

You can not get a pointer to some arbitrary language construct that represents a type.  Period.  No facility exists for that within the entire language.

As has been stated previously, introspection is a very high-level language concept.  A language that's close to the silicon, like C or C++, is unlikely to expose such things.

(And wrapping all your classes to accomplish this idea is rather silly and probably quite inefficient.  If you're doing anything useful with those wrappers, anyhow.)

Name: Anonymous 2006-01-14 3:26

>>28
In that case, how does JAVA managed to do it without implementing a wrapper? I mean, functionality always requires performance-cost, right?

Name: Anonymous 2006-01-14 8:05

>>29
java uses this silly bytecode to store all sorts of ungodly crap. yeah java is pretty damn slow but it has all the bells and whistles you might never use. The only thing C++ and java have in common are the syntax.

You should reconsider redesigning your program so you don't even need to check for the type (ie. you know where everything is and what everything is). Make it more deterministic like it should be written in the first place.

Name: Anonymous 2006-01-14 10:20

>>29

Listen to >>30.  Java stores a boatload of runtime introspection information when it knows you're going to be using reflection, C# does also.  C++ doesn't because there is no introspection. 

I don't mean to be rude, but trying to equate the Java runtime with the C/C++ runtime leads me to believe that you don't really understand the basic fundamentals of either.

Name: Anonymous 2006-01-14 11:53

how to pass a type
#include <typeinfo.h>
idiot_function(type_info t);

idiot_function(typeid(*pA));

now idiot_function can find out whatever the fuck it wants about pA's type. isn't this what you're trying to do?

Name: Anonymous 2006-01-14 12:45

type_info has a private constructor...

Name: Anonymous 2006-01-14 16:14

>>32

No, that's not what the OP is trying to do... Read >>1.  *That's* what the OP is trying to do.

Name: Anonymous 2006-01-14 17:42

>>34
i still don't get u...
type_info has a private constructor...
how can i use type_info if it has a private constructor?

Name: Anonymous 2006-01-14 18:34 (sage)

What do you need the constructor for anyway? You're not supposed to make up your own type_info objects.

Name: Anonymous 2006-01-14 21:34

>>35

>>34 was talking to >>32, not >>33>>34 has nothing to do with type_info and its private constructor.  And >>36 is spot-on, you're not supposed to make your own type_info objects.  Why would you try to?

Name: Anonymous 2006-01-15 6:24

>>37
to make a function like typeid() where I can pass the TYPE as a paramter

Name: Anonymous 2006-01-15 6:48

>>38
You. Can't. Do. That. In. C++.

Get. Over. It.

Name: Anonymous 2006-01-15 9:56

>>39
Proof?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List