Questions tagged [rtti]

RTTI stands for Run-Time Type Information, it is also known as reflection; it allows access to compile-time data at run-time.

RTTI refers to the ability of the system to report on the dynamic type of an object and to provide information about that type at runtime (as opposed to at compile time). When utilized consistently, it can be a powerful tool to ease the work of the programmer in managing resources, or as the basis for writing reflective code. Further explanation can be found on Wikipedia

784 questions
161
votes
11 answers

How expensive is RTTI?

I understand that there is a resource hit from using RTTI, but how big is it? Everywhere I've looked just says that "RTTI is expensive," but none of them actually give any benchmarks or quantitative data reguarding memory, processor time, or…
Cristián Romo
  • 9,206
  • 12
  • 46
  • 50
154
votes
4 answers

is vs typeof

Which of these pieces of code is faster? if (obj is ClassA) {} if (obj.GetType() == typeof(ClassA)) {} Edit: I'm aware that they don't do the same thing.
ilitirit
  • 14,969
  • 17
  • 68
  • 105
107
votes
7 answers

Why is 'pure polymorphism' preferable over using RTTI?

Almost every C++ resource I've seen that discusses this kind of thing tells me that I should prefer polymorphic approaches to using RTTI (run-time type identification). In general, I take this kind of advice seriously, and will try and understand…
mbr0wn
  • 1,222
  • 1
  • 10
  • 10
70
votes
4 answers

What can make C++ RTTI undesirable to use?

Looking at the LLVM documentation, they mention that they use "a custom form of RTTI", and this is the reason they have isa<>, cast<> and dyn_cast<> templated functions. Usually, reading that a library reimplements some basic functionality of a…
zneak
  • 124,558
  • 39
  • 238
  • 307
62
votes
2 answers

When can compiling c++ without RTTI cause problems?

I'm using gcc's -fno-rtti flag to compile my C++ without runtime type information. Assuming I'm not using dynamic_cast<> or typeid(), is there anything that could lead me to later problems?
McPherrinM
  • 4,368
  • 1
  • 19
  • 25
49
votes
9 answers

Practical use of dynamic_cast?

I have a pretty simple question about the dynamic_cast operator. I know this is used for run time type identification, i.e., to know about the object type at run time. But from your programming experience, can you please give a real scenario where…
cexplorer
  • 529
  • 5
  • 13
48
votes
7 answers

Why does typeid.name() return weird characters using GCC and how to make it print unmangled names?

How come when I run this main.cpp: #include #include using namespace std; struct Blah {}; int main() { cout << typeid(Blah).name() << endl; return 0; } By compiling it with GCC version 4.4.4: g++ main.cpp I get…
sivabudh
  • 29,317
  • 56
  • 156
  • 219
43
votes
7 answers

Why should I care about RTTI in Delphi?

I've heard a lot about the new/improved RTTI capabilities of Delphi 2010, but I must admit my ignorance...I don't understand it. I know every version of Delphi has supported RTTI...and I know that RTTI (Runtime Type Information) allows me to access…
Mick
  • 12,760
  • 9
  • 61
  • 118
40
votes
4 answers

Is it safe to use the address of a static local variable within a function template as a type identifier?

I wish to create an alternative to std::type_index that does not require RTTI: template int* type_id() { static int x; return &x; } Note that the address of the local variable x is used as the type ID, not the value of x…
Joseph Thomson
  • 7,955
  • 1
  • 24
  • 34
39
votes
6 answers

dynamic_cast from "void *"

According to this, void* has no RTTI information, therefore casting from void* is not legal and it make sense. If I remember correctly, dynamic_cast from void* was working on gcc. Can you please clarify the issue.
dimba
  • 24,103
  • 28
  • 127
  • 188
37
votes
7 answers

activate RTTI in c++

Can anybody tell me how to activate RTTI in c++ when working on unix. I heard that it can be disabled and enabled. on my unix environment,how could i check whether RTTI is enabled or disabled? I am using the aCC compiler on HPUX.
Vijay
  • 59,537
  • 86
  • 209
  • 308
34
votes
3 answers

std::any without RTTI, how does it work?

If I want to use std::any I can use it with RTTI switched off. The following example compiles and runs as expected also with -fno-rtti with gcc. int main() { std::any x; x=9.9; std::cout << std::any_cast(x) <<…
Klaus
  • 20,019
  • 4
  • 46
  • 90
34
votes
5 answers

Extract C++ template parameters

Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template parameters from an existing type, perhaps using RTTI. For example: typedef std::bitset<16> WordSet; Would it be possible to extract the number 16 in…
cdleary
  • 63,281
  • 49
  • 155
  • 190
31
votes
2 answers

Why use std::type_index instead of std::type_info*

I need to key some data in a map by a type. Currently I have something like this: struct TypeInfoComparer { bool operator()(std::type_info const* a, std::type_info const* b) const { return a->before(*b); }; }; std::map
Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
30
votes
8 answers

What's the difference between public and published class members in Delphi?

Please could someone explain me what's the difference between public and published class members in Delphi? I tried to look at Delphi help and I understand that these members have the same visibility, but I don't understand very well how they differ…
Ondra C.
  • 2,302
  • 3
  • 31
  • 35
1
2 3
52 53