extern template class



 DEVELOP > c-Plus-Plus > extern template class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "arun"
Date: 05 May 2006 04:14:43 PM
Object: extern template class
Hello team,
I know that
extern keyword before template class or function delays the
instantiation of that template
declaration.
However,, my question is does this utility only works for creating the
libraries or can I use
this utility without using any libraries.
For example I have three files.
queue.h
one.cc
two.cc
queue.h
******************
template <class T> class Queue {
public:
Queue() {}
~Queue() {}
T t;
};
extern template class Queue<int>;
*******************************
one.cc looks like
*****************
#include "queue.h"
int set(Queue<int> q) {
return q.t;
}
******************
and two.cc looks like
*******************
using namespace std;
#include <iostream.h>
#include "queue.h"
int main(void) {
Queue<int> q;
int a = set(q);
cout <<"The value of a is " << a << endl;
return 0;
}
******************
The whole code does not compile and I get following error by compiler.
********
undefined referenct to Queue<int>::Queue()
undefined referenct to Queue<int>::~Queue()
in function main.
**********
Kindly tell me what am I doing wrong.
Thanks.
arun
.

User: "Pete Becker"

Title: Re: extern template class 05 May 2006 06:17:42 PM
arun wrote:


I know that

extern keyword before template class or function delays the
instantiation of that template
declaration.

Actually, it doesn't. That's a common extension, and it's just been
approved for the next version of the C++ language definition. But it's
not required in conforming compilers today, so whatever it actually does
is implementation-specific.


However,, my question is does this utility only works for creating the
libraries or can I use
this utility without using any libraries.

The future standardized version can be used anywhere. It declares that a
template instantiation exists but doesn't force it to be instantiated.


The whole code does not compile and I get following error by compiler.

********
undefined referenct to Queue<int>::Queue()
undefined referenct to Queue<int>::~Queue()

This leads to the same problem:
extern int i;
You've said that you're going to provide a defition somewhere, so you
have to do it:
int i;
Similarly,
extern template class Queue<int>;
is a declaration that says that you're going to provide a definition, so
you have to do it:
template class Queue<int>;
--
Pete Becker
Roundhouse Consulting, Ltd.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER