This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Question about C++ and type aliasing


I'm deciding whether to use C or C++ for a high performance simulation
project.  In many ways C++ is a better match for the problem, but I am
concerned about performance.

Specifically, last January there was a thread about C++ and the rules
for type alias analysis.

I wasn't very clear on the outcome of that thread.  If I define a base
class and another that is derived from it, and I use some virtual
functions, will I get code that is as fast as doing the equivalent in C?

I know I can use C and simulate vtables (much as Linux does), I know
that if I do "poor man's inheritance" using C and casts between
structure types, then I expect to get the best available performance
from type alias analysis.  (I don't know how good that is on structures
and structure fields, but at least i know I'll be getting the best GCC
has to offer :-)

These are my questions:

  1. It's possible that I'm violating some type alias rules in C, by
     casting pointers between difference structure types in order to
     implement inheritance.  Does anyone know?

         (a) if struct Derived's first field is of type struct Base (so
         that Derived is effectively a C++-like derived type of Base),
         and I cast between `Derived *' and `Base *', can this misorder
         accesses with strict-aliasing turned on?  (I think the answer
         is no, but I'm not sure).

         (b) if struct Derived's first fields are identical to the
         fields of Base, can this misorder accesses?  (I think the
         answer is yes, but I'm not sure).

  2. If I implement this in C++, using a type Derived which is derived
     from another type, Base, I can implement exactly equivalent code,
     right down to the word layout of the data structures and the
     functions.  But will I get equivalently fast code (particularly
     with respect to type alias analysis),

         (a) if there are no virtual functions in Derived or Base.

         (b) if there are virtual functions in Derived and Base.

     It's ok to assume that exceptions are disabled, and there is
     no multiple or virtual inheritance.

Thanks,
-- Jamie


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]