473,396 Members | 1,655 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,396 software developers and data experts.

How to use template to create a library.

Hi,
I want to create a library out of a C++ sources. The implementation
should be in object files and there will be a header file which the
client will #include. I want to use templates to implement the code.
This is the API in the header file:

void GenericQSort (T A[SIZE], int len);

and clients can call it like :

Mystring A[SIZE];
GenericQSort(A,size);

where Mystring is user defined data type where he should overload <
operator for comparison. If I define a template function for
GenericQSort, any change made to it will require clients to recompile
which I don't want. How should I implement GenericQSort?
Thanks

Mar 29 '07 #1
5 1807
Sunny wrote:
Hi,
I want to create a library out of a C++ sources. The implementation
should be in object files and there will be a header file which the
client will #include. I want to use templates to implement the code.
This is the API in the header file:

void GenericQSort (T A[SIZE], int len);

and clients can call it like :

Mystring A[SIZE];
GenericQSort(A,size);

where Mystring is user defined data type where he should overload <
operator for comparison. If I define a template function for
GenericQSort, any change made to it will require clients to recompile
which I don't want. How should I implement GenericQSort?
a) Bad example: there already is std::sort.

b) Unless you (and all your clients) use a compiler that supports "export",
you implement templated functions and classes within the header files of
your library. If you cannot predict for which types the templates will be
instantiated, there is no other way: the template code has to be visible to
the client that is causing the instantiation.
Best

Kai-Uwe Bux
Mar 29 '07 #2
On Thu, 29 Mar 2007 13:46:25 -0700, Sunny wrote:
Hi,
I want to create a library out of a C++ sources. The implementation
should be in object files and there will be a header file which the
client will #include. I want to use templates to implement the code.
This is the API in the header file:

void GenericQSort (T A[SIZE], int len);

and clients can call it like :

Mystring A[SIZE];
GenericQSort(A,size);

where Mystring is user defined data type where he should overload <
operator for comparison. If I define a template function for
GenericQSort, any change made to it will require clients to recompile
which I don't want. How should I implement GenericQSort?
Thanks
You can't, unless you provide predefined template
instantiations of your GenericQSort for every type your users may attempt
to use GenericQSort on. In order to instantiate a template for a
particular set of parameters, the compiler must have the source for that
template. The compiler uses that source and generates a version of the
method specific to that set of parameters at compile time - it cannot
reference the source in another object file (unless the object file has
an explicit instantiation of the template over those parameters, and the
compiler knows that it's there via an extern declaration of the
instantiation).

- Michael
Mar 29 '07 #3
On Mar 30, 3:52 am, Michael Ekstrand <use...@elehack.netwrote:
On Thu, 29 Mar 2007 13:46:25 -0700, Sunny wrote:
Hi,
I want to create a library out of a C++ sources. The implementation
should be in object files and there will be a header file which the
client will #include. I want to use templates to implement the code.
This is the API in the header file:
void GenericQSort (T A[SIZE], int len);
and clients can call it like :
Mystring A[SIZE];
GenericQSort(A,size);
where Mystring is user defined data type where he should overload <
operator for comparison. If I define a template function for
GenericQSort, any change made to it will require clients to recompile
which I don't want. How should I implement GenericQSort?
Thanks

You can't, unless you provide predefined template
instantiations of your GenericQSort for every type your users may attempt
to use GenericQSort on. In order to instantiate a template for a
particular set of parameters, the compiler must have the source for that
template. The compiler uses that source and generates a version of the
method specific to that set of parameters at compile time - it cannot
reference the source in another object file (unless the object file has
an explicit instantiation of the template over those parameters, and the
compiler knows that it's there via an extern declaration of the
instantiation).

- Michael
OK. If I instantiate the template for a data type then it should work,
although for only that data type. Here is what I tried.
///file: a.hh
#ifndef __A_HH
#define __A_HH
template <typename T>
T sum(T a, T b)
{
return a+b;
}
#endif
///file a.cc
#include "a.hh"
template int sum<int>(int,int);

I compiled it and created a.o: g++ -c a.cc

Now I am trying to call sum in cl.cc

#include <iostream>
// I cannot #include a.hh because I don't want this file to be
recompiled if a.hh changes

using std::cout;
using std::endl;
int main()
{
int a = sum(4,5);
cout << a << endl;
return 0;
}

In this file what should I #include? I cannot #include a.hh. The
function implementation is present in a.o but how can I declare the
function here?
Thanks

Mar 30 '07 #4
On Fri, 30 Mar 2007 00:55:05 -0700, Sunny wrote:
In this file what should I #include? I cannot #include a.hh. The
function implementation is present in a.o but how can I declare the
function here?
You need to do one of two things: have 2 header files, or move your
template code into your .cpp. Here's what I have for your example:

/* BEGIN sum.hh */
template<typename TT sum(T a, T b); // notice declaration only
extern template int sum<int>(int, int); // extern decl of instantiation
/* END sum.hh */

/* BEGIN sum.cc */
#include "tmpl.h" // probably don't actually need this

// define the template...
template<typename TT sum(T a, T b)
{
return a+b;
}

// and instantiate it
template int sum<int>(int, int);
/* END sum.cc */

/* BEGIN main.cc */
#include "sum.hh"
#include <iostream>

int main()
{
std::cout <<"1 + 2 = " <<sum(1, 2) <<std::endl;
return 0;
}
/* END main.cc */

- Michael
Mar 30 '07 #5
On Mar 30, 5:11 pm, Michael Ekstrand <use...@elehack.netwrote:
On Fri, 30 Mar 2007 00:55:05 -0700, Sunny wrote:
In this file what should I #include? I cannot #include a.hh. The
function implementation is present in a.o but how can I declare the
function here?

You need to do one of two things: have 2 header files, or move your
template code into your .cpp. Here's what I have for your example:

/* BEGIN sum.hh */
template<typename TT sum(T a, T b); // notice declaration only
extern template int sum<int>(int, int); // extern decl of instantiation
/* END sum.hh */

/* BEGIN sum.cc */
#include "tmpl.h" // probably don't actually need this

// define the template...
template<typename TT sum(T a, T b)
{
return a+b;

}

// and instantiate it
template int sum<int>(int, int);
/* END sum.cc */

/* BEGIN main.cc */
#include "sum.hh"
#include <iostream>

int main()
{
std::cout <<"1 + 2 = " <<sum(1, 2) <<std::endl;
return 0;}

/* END main.cc */

- Michael
Thanks for the code Michael. This solves my problem.

Mar 30 '07 #6

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

Similar topics

1
by: John Dumais | last post by:
Hello, I have a templated class defined in a header file and implemented in a cpp source file. The class is basically a little marshaler that contains a factory method and a few marshaling...
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
4
by: Michael Maes | last post by:
Hello, I want to add a date (ShortDateString) to a custom template-file (Class). I use: a.. b.. c.. But I can't find anythig on a date (something like: )
1
by: Vikash | last post by:
Say Im going to create a new class called "master.cs" if i created this file , by default file and author information should be added at the top of the file. Example ...
35
by: Steven T. Hatton | last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth of the matter, but right now I have some serious misgivings about the value of investing a lot of time and effort into...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
1
by: WebCM | last post by:
I'm looking for a good idea or ready library for templates. HTML with PHP isn't the best solution (it's more difficult for end-users of CMS to edit them). Perhaps, everything I need is: -...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.