Explicit instantiation of incomplete template classes

J

JurgenvonOerthel

Consider the following code (in three files: base.h, base.cc &
main.cc):

base.h:
---------
template <typename T> class Base {
public :
virtual ~Base();
};

base.cc:
----------
#include "base.h"

template class Base<int>; // NOTE: Definition of type Base is
incomplete!

template <class T>
Base<T>::~Base() {
}

main.cc:
-----------
#include "base.h"

template <typename T> class Derived : public Base<T> {
public:
virtual ~Derived() {}
};

template class Derived<int>;

int main () {
Derived<int> p;
return 0;
}

Compile:
-----------
g++ -c base.cc -g -W -Wall -m32 -fno-implicit-templates -std=c++98 -o
base.o
g++ -c main.cc -g -W -Wall -m32 -fno-implicit-templates -std=c++98 -o
main.o
g++ -m32 base.o main.o

===================
With g++ 3.4.4 this compiles and links without problems.
With g++ 4.2.2 this causes link errors because the destructors of
Base<int> are not generated in Base.o. No diagnostics are produced by g
++ however.
When I move the instantiation of Base<int> after the definition of the
destructor it compiles and links fine.

My question is:
Is explicitly instantiating an incomplete template class allowed,
undefined behaviour or is the program ill-formed?
 
J

JurgenvonOerthel

In the standard I've only found section 14.7.2:3
"A definition of a class template or class member template shall be in
scope at the point of the explicit instantiation of the class template
or class member template"

I'm not sure this means that if a class template is not defined
completely that it's UB.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top