How do I manually instantiate template functions with -fno-implicit-templates
Mark Mitchell
mmitchell@usa.net
Wed Jan 7 09:59:00 GMT 1998
Here is a test case for what I need:
test.h:
#include<iostream.h>
struct test_box
{
void print(void) {cout << "this is a test" << endl;}
};
void test(test_box *); // Just a forward declaration
It is this line that is the problem. You should have written:
template <class BOX> void test(BOX* the_box);
here. A long explanation below.
test.C:
#include "test.h"
template <class BOX> void test(BOX *the_box)
{
the_box->print();
};
template void test<> (test_box *); // This is the guiding decl
main.C:
#include "test.h"
main()
{
test_box box1;
test(&box1);
}
First, the use of -fno-implicit-templates makes no difference in this
particular case, so let's not worry about it. If we look at (some of)
the symbols in each .o:
supernova% nm test.o | test-c++filt
00000000 T void test<test_box>(test_box *)
supernova% nm main.o | test-c++filt
U test(test_box *)
We can see what's happenning. The main.C file is trying to call a
non-template function; the test.C file defines (and causes the
instantiation of) a template function. They don't match, according to
the Standard. That's why the prototype should be a template
prototype, not a non-template prototype.
--
Mark Mitchell mmitchell@usa.net
Stanford University http://www.stanford.edu
More information about the Gcc
mailing list