S
Steve Fischer
If I have the following:
class dataclass1
{
// ...
};
class dataclass2
{
// ...
};
class abstractbase1
{
public:
abstractbase1();
virtual ~abstractbase1();
virtual func1() = 0;
virtual func2() = 0;
};
template<class R> class templateclass1 : public abstractbase1
{
public:
templateclass1(typename R& data1) : m_data(data1) { }
virtual ~templateclass1() {}
protected:
typename R& m_data;
};
class derived2 : public templateclass1<dataclass1>
{
public:
derived2(dataclass1& data) :
templateclass1<dataclass1>(data)
virtual ~derived2() {}
virtual func1();
virtual func2();
};
class derived3 : public templateclass1<dataclass2>
{
public:
derived3(dataclass1& data) :
templateclass1<dataclass1>(data)
virtual ~derived3() {}
virtual func1();
virtual func2();
};
Now suppose I have 20 more classes like derived2 which instantiate
templateclass1 with dataclass1, and 20 more classes like derived3
which instantiate templateclass1 with dataclass2.
The question is: would the compiler typically instantiate
templateclass1<dataclass1> 21 times and templateclass1<dataclass2> 21
times (once for each derived class), resulting in large amounts of
duplicate code, or would the compiler only instantiate templateclass1
once for each unique parameter with which it is supplied? If
templateclass1<dataclass1> is instantiated 21 times, is there any way
to avoid this?
TIA,
sf
class dataclass1
{
// ...
};
class dataclass2
{
// ...
};
class abstractbase1
{
public:
abstractbase1();
virtual ~abstractbase1();
virtual func1() = 0;
virtual func2() = 0;
};
template<class R> class templateclass1 : public abstractbase1
{
public:
templateclass1(typename R& data1) : m_data(data1) { }
virtual ~templateclass1() {}
protected:
typename R& m_data;
};
class derived2 : public templateclass1<dataclass1>
{
public:
derived2(dataclass1& data) :
templateclass1<dataclass1>(data)
virtual ~derived2() {}
virtual func1();
virtual func2();
};
class derived3 : public templateclass1<dataclass2>
{
public:
derived3(dataclass1& data) :
templateclass1<dataclass1>(data)
virtual ~derived3() {}
virtual func1();
virtual func2();
};
Now suppose I have 20 more classes like derived2 which instantiate
templateclass1 with dataclass1, and 20 more classes like derived3
which instantiate templateclass1 with dataclass2.
The question is: would the compiler typically instantiate
templateclass1<dataclass1> 21 times and templateclass1<dataclass2> 21
times (once for each derived class), resulting in large amounts of
duplicate code, or would the compiler only instantiate templateclass1
once for each unique parameter with which it is supplied? If
templateclass1<dataclass1> is instantiated 21 times, is there any way
to avoid this?
TIA,
sf