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 5 1825
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
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
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,in t);
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
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<typena me 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<typena me 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
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<typena me 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<typena me 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 functions. The template parameter
defines the type stored in a contained vector. That class is built into
a
shared library. The (condensed) code goes like this...
|
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:
template< class T<int N> >
int func()
{
int the_n = N;
|
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: )
|
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
-------------------------------------------------------------------------
Author :
|
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 template programming. I just
finished reading the first 12 chapters of _C++ Templates: The Complete
Guide_. One thing that struck me as I read these chapters is how much I
didn't know about C++. I _know_ the core language specification is...
| |
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 cross-platform scripting
engine. When the scripting engine is embedded in an application, a
platform-specific support library is linked in.)
My first attempt goes here:
---code begin (library)---
|
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>
class WidgetManager : public Creation Policy {...}
// Create an instance of WidgetManager which managers type Widget
WidgetManager< OpNewCreator<Widget MyWidgetMgr;
|
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:
- variables - e.g. {var}
- sections or conditions (some elements won't be printed out)
- loops or selecting fragments*
- cache (recommended for speed)
The template system must be fast and efficient. Most probably I will
create own library for CMS...
|
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>
|
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,...
|
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...
| |
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...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |