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

Pages: 1-4041-

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?

Name: Anonymous 2006-01-15 10:19

I don't know C++ but I googled 'c++ type argument' and there was much about templates and 'List<T>' and 'Template non-type arguments (C++ only)'. I will assume that C++ can take types as argument during compile time when macros and templates are being evaluated.

Name: Anonymous 2006-01-15 11:35

I hereby officially declare the OP or whomever continues insisting that "there must be a way" to be a troll.  No non-troll would continue to be this stupid.

Name: Anonymous 2006-01-15 11:48

>>42
seconded

Name: Anonymous 2006-01-15 15:16

Fine then.
Lets say that C++ can't acomplish that
using macros and templates.

In that case, how about overloading typeid?
I'm sure THAT's possible, right?

There is always more than one way to skin a cat.
Its just a matter of creativity
 

Name: Anonymous 2006-01-15 16:06

>>44
You are either mentally retarded or one of most persistent trolls evar.

Name: Anonymous 2006-01-15 17:59

>>44
Next up, there MUST be a way to solve the travelling sales person problem in linear time right?

Name: Anonymous 2006-01-15 19:45

>>44

And there MUST be a way to solve the halting problem, right?

Name: Anonymous 2006-01-15 20:13

ITT we will prove P != NP:

Name: Anonymous 2006-01-16 3:22

>>46
>>47
>>48
I love you guys.

Name: Anonymous 2006-01-16 4:29

>>22
Lower level languages have less features like that. You can't expect to know implementation details at run time.

So you don't do that. If you need to, think again. If you *really* *really* *pretty really* need to, user a higher level language and be prepared for a performance loss which you usually can afford. I discourage Java, it sucks. Try Python.

Name: Anonymous 2006-01-16 5:43

>>50
Oh really?
If lower level languages
can't allow us to know implementation details at run time,
then how come typeid() does exactly that?

Come on man!
Lower level languages allows us to do MORE stuff
than higher level language.
Take JAVA and C++ for example.
JAVA can't communicate with hardware without C++ dlls.
HAH! SO THERE!

C++ RULZ!!!

There MUST be a way. U guys just suck.

Name: Anonymous 2006-01-16 5:52 (sage)

>>50

Dammit, dude, stop feeding the troll.

Name: Anonymous 2006-01-16 7:46

>>52
I'm not a troll, I'm a C++ programmer!
HAH! SO THERE!

Name: Anonymous 2006-01-16 8:21 (sage)

>>51,53
lol

Name: Anonymous 2006-01-16 9:06

It's not whether the language is higher level or not. It just depends on whether the language has a good macro language (when the language is static type).

Name: Anonymous 2006-01-16 9:16

>>55
Lisp is the highest level language of them all. It also has the most powerful macros. In fact once you've used Lisp macros you wonder how you ever got along with all that shitty #define crap.

Name: Anonymous 2006-01-16 9:23

>>56
Yes but I meant that C could easily get a good macro language that recognises types, etc.

Name: Anonymous 2006-01-16 10:37 (sage)

>>57
Gee, that almost sounds like Objective C. Except not.

Name: Anonymous 2006-01-16 12:57

>>51
PRRRRRRROOOOOOOOOGGGGGGG

Name: Anonymous 2006-01-19 5:49

bump

Name: Anonymous 2006-01-19 7:16 (sage)

Mmmm, yummy, troll bumpage.

Name: Anonymous 2006-01-19 7:49 (sage)

Name: Anonymous 2006-02-02 3:37 (sage)

>>38
I have no idea why you want to do that anyways. Get over it. What about the data? Just overload your function if you want to be able to call it with different types. If you're working with Variant-style data, you've already got some kind of boost::any thing going on. Just keep using it.

Name: Anonymous 2010-01-27 14:41

#define int m_class
where m_class contains an integer, and type info about it.
I DID IT, I FIX'D THIS NECRO THREAD!

Name: Anonymous 2010-01-27 15:34

<<64 DONT DO THAT it breaks my SFINAE based metaprogramming reusable libraries depending on member method pointer overload resolution precedence differences between builtin and user defined types

what OP needs is clearly:

1: get a list of all used types (for example: A,B,C,D,...)
2:
/*returns the type, as string*/ template<T*> std::string typeID(T* buio){return dynamic_cast<A*>(buio)?"A":(dynamic_cast<B*>(buio)?"B":(dynamic_cast<C*>(buio)?"C":(dynamic_cast<D*>(buio)?"D":(dynamic_cast<E*>(buio)?"E":(/*more of this, also dont forget additional FV-izeation for production code*/)))));}

Name: Anonymous 2010-01-27 15:58

Depending on what you want to do, this is possible using type_traits from the boost library.

Name: Anonymous 2010-01-27 16:05

>>65
what OP needs is clearly:
2006-01-11 12:59

Name: Anonymous 2010-01-28 5:00

Either:

#define f(x) _f(typeid(*x))
void f(type_info t) { /* stuff */ }

or

#define f(x) _f<x>()
template<class F>
void f() { /* stuff */ };

but you should probably stick to vanilla templates to avoid confusion unless you really need to confuse things.

Name: Anonymous 2010-11-26 3:25

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