A problem about explicit instantiation of class template

Y

yuanhp_china

I define a class in A.h:

template <class T> class A{

public:
void get_elem( const T&) ;
};



Then I give the implementation in A.cpp (and do an explicit
instantiation ):

template<class T> void A::get_elem( const T&)
{...}

template class A<int>; //explicit instantiation




I have another two files: main.cpp and B.cpp. I would like to reuse
the explicit instantiation of A<int> of A.cpp in the two files. Do I
need to declare class A<int> in the two files? I am not sure how to do
it?

Any help are appreciated.


Hua
 
B

Barry

I define a class in A.h:

template <class T> class A{

public:
void get_elem( const T&) ;
};



Then I give the implementation in A.cpp (and do an explicit
instantiation ):

Separating a template class into hxx file and cxx file should be done
with keyword 'export' support, which is rarely supported by compilers
(IIRC, Comeau supports it).

So when you define a template class, just put all the implementation in
hxx files.
template<class T> void A::get_elem( const T&)
{...}

template class A<int>; //explicit instantiation




I have another two files: main.cpp and B.cpp. I would like to reuse
the explicit instantiation of A<int> of A.cpp in the two files. Do I
need to declare class A<int> in the two files? I am not sure how to do
it?

Don't worry about the duplicated instantiation, the compiler is smart
enough to know which she's already instantiated.
Any help are appreciated.

I think you should find a textbook to start with template programming first.
 
S

Salt_Peter

I define a class in A.h:

template <class T> class A{

public:
void get_elem( const T&) ;

};

Then I give the implementation in A.cpp (and do an explicit
instantiation ):

template<class T> void A::get_elem( const T&)
{...}

template class A<int>; //explicit instantiation

I have another two files: main.cpp and B.cpp. I would like to reuse
the explicit instantiation of A<int> of A.cpp in the two files. Do I
need to declare class A<int> in the two files? I am not sure how to do
it?

Any help are appreciated.

Hua


Have you consulted the FAQ?
[35.15] How can I avoid linker errors with my template classes?
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15
 
Q

qqq

Separating atemplateclassinto hxx file and cxx file should be done
with keyword 'export' support, which is rarely supported by compilers
(IIRC, Comeau supports it).

So when you define atemplateclass, just put all the implementation in
hxx files.






Don't worry about the duplicatedinstantiation, the compiler is smart
enough to know which she's already instantiated.


I think you should find a textbook to start withtemplateprogramming first.

Thanks for your reply. I have read many c++ books, but no one talk
the explicit instantiation very well.

I prefer to the explicit instantiation instead of inline. But when I
do an explicit instantiation in one .cpp file, I got some link error
when more than one files are needing to use the explicit instantiation
(if just one file, it can work.).
 
B

Barry

qqq said:
Thanks for your reply. I have read many c++ books, but no one talk
the explicit instantiation very well.

I prefer to the explicit instantiation instead of inline. But when I
do an explicit instantiation in one .cpp file, I got some link error
when more than one files are needing to use the explicit instantiation
(if just one file, it can work.).

I didn't mean reading a textbook about "template explicit
instantiation", which is quite a rarely(at least for me) used language
feature.

http://www.codeproject.com/KB/cpp/templatesourceorg.aspx
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top