473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A problem about explicit instantiation of class template

I define a class in A.h:

template <class Tclass A{

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

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

template<class Tvoid 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<intof A.cpp in the two files. Do I
need to declare class A<intin the two files? I am not sure how to do
it?

Any help are appreciated.
Hua


Jan 1 '08 #1
4 2459
yu**********@hotmail.com wrote:
I define a class in A.h:

template <class Tclass 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 Tvoid 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<intof A.cpp in the two files. Do I
need to declare class A<intin 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.
Jan 1 '08 #2
On Jan 1, 1:35 am, yuanhp_ch...@hotmail.com wrote:
I define a class in A.h:

template <class Tclass A{

public:
void get_elem( const T&) ;

};

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

template<class Tvoid 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<intof A.cpp in the two files. Do I
need to declare class A<intin 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
Jan 1 '08 #3
qqq
On Jan 1, 3:24*am, Barry <dhb2...@gmail.comwrote:
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<classTvoid 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<intof A.cpp in the two files. Do I
need to declareclassA<intin 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.).
Jan 1 '08 #4
qqq wrote:
On Jan 1, 3:24 am, Barry <dhb2...@gmail.comwrote:
>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<classTvoid 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<intof A.cpp in the two files. Do I
need to declareclassA<intin 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
Jan 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy | last post by:
Hi, I got a little confused on 'instantiation' and 'specialization', espcially for explicit instantiation and explicit sepcialization. Can anybody explain the difference? Thanks a lot! ...
6
by: Thomas Maier-Komor | last post by:
Hi everybody, I am a little bit confused with the syntax of explicit instantiation, and I am not sure that it is possible to do what I want to do. Maybe someone has an idea. Consider a...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
5
by: henkoo | last post by:
i want to explicit instantiate a member function of a class template, and got some error, can anyone give some advice to correct it? 3x complier: g++ 3.2 #include <iostream> #include...
5
by: Avner | last post by:
Hi, I am creating a template library for a template class with two template arguments. Is there a way to span a suite of explicit instantiation, instead of having to specify all the...
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.