RTTI Overhead in C++

前端 未结 4 2313
感情败类
感情败类 2020-12-15 05:46

What are the memory/performance overheads of enabling RTTI in a C++ program?
Can anyone please throw some light between the internal implementation of RTTI mechanism and

4条回答
  •  庸人自扰
    2020-12-15 06:09

    Enabling RTTI typically brings only a small overhead. The usual implementation carries a pointer to the type information structure in the vtable of an object. Since the vtable must be constructed anyway, the extra time is small - it's like adding another virtual function to the class.

    typeid is therefore comparable to calling a virtual function. dynamic_cast is slower - it needs to traverse the inheritance hierarchy to do a cast. Calling dynamic_cast too frequently can be a performance bottleneck. By 'can' I mean that it usually won't …

    There is a slight bloat in executable size since the typeinfo structures need to be stored somewhere. In most cases it won't be relevant.

提交回复
热议问题