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]

GCC C++ & inlining problem


There seems to be a number of problems with GCC inlining class member
functions in C++ code,
it is not clear if these are different manifestations of a single problem or
are separate problems,
however they appear to be serious errors resulting in significant code
growth and sometimes making
it impossible to link the resulting object files.



Assume we have a class ClassA declared in a file and we instantiate the
class and give it a value in the same file

test.cpp

class ClassA{
int Value;
public:
ClassA(){}
~ClassA(){}
ClassA & operator=(const int op1){
Value = op1;
return *this;
}
ClassA & operator*=(const int op2){
Value *= op2;
return *this;
}
};


void test(void){
ClassA x;
x = 1;
}


this compiles OK and produces a small executable about 2K bytes.

Now if we move the class to ClassA.h and include it we get the same results.

Next, split the class declaration into ClassA.h and it's definition also in
ClassA.cpp as so

ClassA.h
class ClassA{
int Value;
public:
ClassA();
~ClassA();
ClassA & operator=(const int op1);
ClassA & operator*=(const int op2);

};

ClassA::ClassA(){}
ClassA::~ClassA(){}
ClassA & ClassA::operator=(const int op1){
Value = op1;
return *this;
}
ClassA & ClassA::operator*=(const int op2){
Value *= op2;
return *this;
}

test.cpp
#include "ClassA.h"
void test(void){
ClassA x;
x = 1;
}

This still compiles Ok and produces a 2K object file.


now we move the class definition into ClassA.cpp
ClassA.h
class ClassA{
int Value;
public:
ClassA();
~ClassA();
ClassA & operator=(const int op1);
ClassA & operator*=(const int op2);

};

ClassA.cpp
#include "ClassA.h"
ClassA::ClassA(){}
ClassA::~ClassA(){}
ClassA & ClassA::operator=(const int op1){
Value = op1;
return *this;
}
ClassA & ClassA::operator*=(const int op2){
Value *= op2;
return *this;
}

test.cpp
#include "ClassA.h"
void test(void){
ClassA x;
x = 1;
}


re-compile and the code size increases to about 25 K bytes.

next, try making the functions inline by adding the inline keyword
ClassA.cpp
#include "ClassA.h"
inline ClassA::ClassA(){}
inline ClassA::~ClassA(){}
inline ClassA & ClassA::operator=(const int op1){
Value = op1;
return *this;
}
inline ClassA & ClassA::operator*=(const int op2){
Value *= op2;
return *this;
}


the program will no longer link, all of the operators and constructors of
class A are now unresolved.


Taking this further, if we define a class ClassB which instantiates member
variables of type ClassA,

if we define and declare ClassA entirely in ClassA.h, and ClassB entirely in
ClassB.h then everything is fine and
we get a small executable.

If we then revert to the 2 file version for ClassA (with or without without
the inline specifier), the program will
no longer link since all references by ClassB to ClassA are unresolved

If we also change ClassB to a 2 file version and do not specify 'inline' for
either then the program will link
but has again grown to over 25K.













--
Joel Brenner -- Designer E-mail: joel.brenner@nemerix.com
Phone: +41 91 612 46 00 -- Fax : +41 91 612 47 01
Direct: +41 91 612 47 12
www.nemerix.com

NemeriX SA Stabile Gerre 2000
PO Box 425 6928 Manno, Switzerland
----------




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