A problem about explicit instantiation of class template



 DEVELOP > c-Plus-Plus > A problem about explicit instantiation of class template

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 01 Jan 2008 12:35:34 AM
Object: A problem about explicit instantiation of class template
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
.

User: "Barry"

Title: Re: A problem about explicit instantiation of class template 01 Jan 2008 02:24:58 AM
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.
.
User: "qqq"

Title: Re: A problem about explicit instantiation of class template 01 Jan 2008 12:44:53 PM
On Jan 1, 3:24=A0am, Barry <dhb2...@gmail.com> wrote:

yuanhp_ch...@hotmail.com wrote:

I define aclassin A.h:


template<classT>classA{


public:
=A0 =A0 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&)
=A0{...}


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.).
.
User: "Barry"

Title: Re: A problem about explicit instantiation of class template 01 Jan 2008 07:58:59 PM
qqq wrote:

On Jan 1, 3:24 am, Barry <dhb2...@gmail.com> wrote:

yuanhp_ch...@hotmail.com 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/templatesourceorg.aspx
.



User: "Salt_Peter"

Title: Re: A problem about explicit instantiation of class template 01 Jan 2008 08:53:14 AM
On Jan 1, 1:35 am,
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-lite/templates.html#faq-35.15
.


  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