Explicit instantiation of incomplete template classes

Discussion in 'C++' started by JurgenvonOerthel@hotmail.com, Jan 21, 2008.

  1. Guest

    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?
     
    , Jan 21, 2008
    #1
    1. Advertising

  2. Guest

    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.
     
    , Jan 21, 2008
    #2
    1. Advertising

  3. Guest

    Bump
     
    , Jan 22, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Cuenca
    Replies:
    4
    Views:
    2,529
    Gianni Mariani
    Sep 6, 2004
  2. Mikhail N. Kupchik
    Replies:
    4
    Views:
    3,162
    Jonathan Turkanis
    Sep 15, 2004
  3. Thomas Maier-Komor
    Replies:
    6
    Views:
    632
    Thomas Maier-Komor
    May 19, 2005
  4. Replies:
    1
    Views:
    580
    Salt_Peter
    Dec 25, 2006
  5. Noah Roberts
    Replies:
    6
    Views:
    1,174
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page