Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A problem about explicit instantiation of class template

Reply
Thread Tools

A problem about explicit instantiation of class template

 
 
yuanhp_china@hotmail.com
Guest
Posts: n/a
 
      01-01-2008
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






 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      01-01-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.


 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      01-01-2008
On Jan 1, 1:35 am, (E-Mail Removed) wrote:
> 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-lit...html#faq-35.15
 
Reply With Quote
 
qqq
Guest
Posts: n/a
 
      01-01-2008
On Jan 1, 3:24*am, Barry <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I define aclassin A.h:

>
> >template<classT>classA{

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

>
> > Then I give the implementation in A.cpp (and do anexplicit
> >instantiation):

>
> 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.
>
>
>
> >template<classT> void A::get_elem( const T&)
> > *{...}

>
> >templateclassA<int>; //explicitinstantiation

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

>
> Don't worry about the duplicatedinstantiation, 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 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.).
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      01-02-2008
qqq wrote:
> On Jan 1, 3:24 am, Barry <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> I define aclassin A.h:
>>> template<classT>classA{
>>> public:
>>> void get_elem( const T&) ;
>>> };
>>> Then I give the implementation in A.cpp (and do anexplicit
>>> instantiation):

>> 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.
>>
>>
>>
>>> template<classT> void A::get_elem( const T&)
>>> {...}
>>> templateclassA<int>; //explicitinstantiation
>>> I have another two files: main.cpp and B.cpp. I would like to reuse
>>> theexplicitinstantiationof A<int> of A.cpp in the two files. Do I
>>> need to declareclassA<int> in the two files? I am not sure how to do
>>> it?

>> Don't worry about the duplicatedinstantiation, 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 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.).


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/te...sourceorg.aspx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of non-template member of template class Noah Roberts C++ 6 02-02-2011 09:53 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
?explicit instantiation member function of class template henkoo@gmail.com C++ 5 12-04-2005 04:06 PM
explicit instantiation of template methods of template classes Thomas Maier-Komor C++ 6 05-19-2005 08:00 AM
Explicit template instantiation from template function doesn't compile? Fernando Cuenca C++ 4 09-06-2004 04:06 PM



Advertisments