Stopping template instantiation

James Mclaren james@q-games.com
Mon Jun 10 23:00:00 GMT 2002




Hi, I've got some template member functions that I'm explicitly
instantiating, similar to the following:

//// Frog.h

class Frog
{
public:
    template <class P>
    static void jump( P& var);
};



//// Frog2.h

#include "Frog.h"

template< class P>
void Frog::jump(P& var)
{
public:
    var.somefunc();
};



//// Lilly.h

class Lilly
{
public:
    void somefunc();
    void someOtherFunc();
};



//// Pond.h
class Lilly;

class Pond
{
public:
    void somefunc();
};


//// FrogLillyPond.cpp

#include "Frog.h"
#include "Pond.h"

class Lilly;

void testFunc(Pond& _Pond, Lilly& _Lilly)
{
    Frog<Pond> frog1;
    Frog<Lilly> frog2;

    _Lilly.someOtherFunc();
    frog1.jump(_Pond);
    frog2.jump(_Lilly);
};

#include "Frog2.h"
template void Frog::jump(Pond&); // explicitly instantiate for Pond
// Lilly instantiated elsewhere




But I get a problem, because the compiler tries to instantiate the "jump"
member function for both classes, even though I only want it instantiated
for both. I know the example is a little contrived, but the problem is in
some smart pointer code that I'm writing, and posting all the code would be
really confusing . I want to be able use part of the functionality of the
smart pointer, without having to include the header of the class I'm
pointing to (Hence the reason for defining the template member seperately in
Frog2.h).
    Is there a way to stop the compiler for attempting to perform the
instantiation for the Lilly class?Otherwise I have to include its header.

                Thanks in advance,

                        James.



More information about the Gcc-help mailing list