473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2490
yu**********@ho tmail.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...@ho tmail.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...@ho tmail.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 atemplateclassi nto 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>; //explicitinstant iation
I have another two files: main.cpp and B.cpp. I would like to reuse
theexplicitinst antiationof A<intof A.cpp in the two files. Do I
need to declareclassA<i ntin the two files? I am not sure how to do
it?

Don't worry about the duplicatedinsta ntiation, 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 withtemplatepro gramming 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...@h otmail.com wrote:
>>I define aclassin A.h:
template<clas sT>classA{
public:
void get_elem( const T&) ;
};
Then I give the implementation in A.cpp (and do anexplicit
instantiation ):
Separating atemplateclassi nto 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<clas sTvoid A::get_elem( const T&)
{...}
templateclass A<int>; //explicitinstant iation
I have another two files: main.cpp and B.cpp. I would like to reuse
theexplicitin stantiationof A<intof A.cpp in the two files. Do I
need to declareclassA<i ntin the two files? I am not sure how to do
it?
Don't worry about the duplicatedinsta ntiation, 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 withtemplatepro gramming 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
2239
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! Andy
6
2014
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 template class: template <class ABC> struct CT : public ABC
5
2084
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 works fine but there in one thing that I'm unsure about and that is the inheritance statement. What difference is it if I have this construction class CheckedArray : public Array<T>
5
2661
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 <string> using namespace std;
5
1441
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 combinations of the template arguments specifically? Say I have a class
3
2443
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 some_func(T const& x) { }
3
4007
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 handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
1
3074
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
2925
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 instantiation of the partially specialized version, but my compiler (VC8) complains because it fails the SFINAE version. I just realized as I was typing this that I'm using partial _function_ specialization. I'm sure I remember reading somewhere that
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.