|
|
Problems with Templates with Shared libraries
I have two functions in my shared library which are declared as
follows:
void setName(const std::string& str);
std::vector<std::string> getInfo();
Since the code is compiled and in shared library. Every thing is great
if the customer of my shared library is using the same compiler
version (and same STL version). I am wondering if the customer changes
their compiler version (or STL version) which may have same interface
for all the standard containers, but different implementations and
sizes; will it still work with my shared library?
Here's an example:
The first function I mentioned above takes a const std::string as an
argument. The compiler I used to build my library (say for example)
defines the std::string something like this (I've omitted the
interface and namespace for simplicity)
template<typename T>
class basic_string
{
T* buffer;
size_t size;
public:
// public interface for std::string goes here
};
typedef basic_string<char> string; // string I used in my first
function
Now say the customer is using an updated version of the same compiler
or some other compiler which defines the std::string by adding an
additional private variable 'count' for reference counting as follows:
template<typename T>
class basic_string
{
T* buffer;
size_t size;
unsigned long* count;
public:
// public interface for std::string goes here
};
Problem:
As you can see the customer passes the new string to my function which
has different size. The function I wrote originally expected a
std::string of a smaller size. Will this break my shared library???
Same thing is also can be said about the second function which returns
a vector of strings, but container returned might be different as what
compiler expects it to be if they are of different sizes.
What I am getting at is that is it a bad idea to use standard
containers in the interface for shared libararies? Since the standard
containers are Templated and not compiled when used by the user of a
shared library.
I hope you understand the problem. Any response will be greatly
appreciated.
Thank you,
Adnan
|
|
0
|
|
|
|
Reply
|
decent4u (1)
|
11/14/2003 8:46:23 PM |
|
AH wrote:
> I have two functions in my shared library which are declared as
> follows:
>
> void setName(const std::string& str);
> std::vector<std::string> getInfo();
>
> Since the code is compiled and in shared library. Every thing is great
> if the customer of my shared library is using the same compiler
> version (and same STL version). I am wondering if the customer changes
> their compiler version (or STL version) which may have same interface
> for all the standard containers, but different implementations and
> sizes; will it still work with my shared library?
>
> Here's an example:
> The first function I mentioned above takes a const std::string as an
> argument. The compiler I used to build my library (say for example)
> defines the std::string something like this (I've omitted the
> interface and namespace for simplicity)
>
> template<typename T>
> class basic_string
> {
> T* buffer;
> size_t size;
> public:
> // public interface for std::string goes here
> };
>
> typedef basic_string<char> string; // string I used in my first
> function
>
>
> Now say the customer is using an updated version of the same compiler
> or some other compiler which defines the std::string by adding an
> additional private variable 'count' for reference counting as follows:
>
> template<typename T>
> class basic_string
> {
> T* buffer;
> size_t size;
> unsigned long* count;
> public:
> // public interface for std::string goes here
> };
>
> Problem:
>
> As you can see the customer passes the new string to my function which
> has different size. The function I wrote originally expected a
> std::string of a smaller size. Will this break my shared library???
>
> Same thing is also can be said about the second function which returns
> a vector of strings, but container returned might be different as what
> compiler expects it to be if they are of different sizes.
>
> What I am getting at is that is it a bad idea to use standard
> containers in the interface for shared libararies? Since the standard
> containers are Templated and not compiled when used by the user of a
> shared library.
>
> I hope you understand the problem. Any response will be greatly
> appreciated.
>
> Thank you,
>
> Adnan
You will have the same problem with any user defined types. If you are
passing user defined types from/to the client (user of the shared
library), the client needs to know the size of the user defined type. So
if you change the implementation of the user defined type in the shared
library, it will cause problems in the client.
To get over this problem, instead of using user defined types for
communication between library and client , use interfaces . Also to
create objects in the shared library use class factory pattern.
Hope this helps.
|
|
0
|
|
|
|
Reply
|
amitgulati (14)
|
11/14/2003 9:13:13 PM
|
|
|
1 Replies
18 Views
(page loaded in 0.071 seconds)
Similiar Articles: templates + RTTI + shared library = impossible? - comp.lang.c++ ...As someone else pointed out, you need to explicitly instantiate the template for the specific type in the shared lib. The problem with templates and shared libraries ... Linker problem - comp.unix.solarisMost link problems I had were shared libraries with lazy set up dependencies. ... openGL example found in the folder that came w/dev-c (Dev-Cpp\Templates ... STL allocators, global new/delete using the heap and shared memory ...They seem to think the problem is solved by declaring the STL variable ... templates + RTTI + shared library = impossible? - comp.lang.c++ ..... this may or may not also ... How to suppress template specialization? - comp.lang.c++.moderated ...I wonder how they solve the problem of interprocess transport of bool ... templates + RTTI + shared library = impossible? - comp.lang.c++ ... How to suppress template ... Non-member operator overloading, linker complains - comp.lang.c++ ...Problem is, that U don't specify body of friend Matrix<T, d, e> operator ... templates + RTTI + shared library = impossible? - comp.lang.c++ ..... tweak GCC's visibility ... segmentation fault when shared object using STL is statically ...This module is now built as shared library ... Will there be any problems when > memory ... with C++, where much of the library consists of inline functions and templates ... Need to find non-exported symbols of DLL(PE) - comp.os.ms-windows ...Hello, I am having big problem as challange to learn. How can I be able ... templates + RTTI + shared library = impossible? - comp.lang.c++ ... so, alas, DLL or shared-library ... Adding an extension to arcmap trouble - comp.soft-sys.gis.esri ...Failed to load Oracle extension and/or shared libraries ... ... "Zero-size" members with templates - comp.lang.c++ ... Problem with ArcMap 10 hanging - Forums... ArcGIS 10 ... How to disable AAAA queries by resolver? - comp.unix.solaris ...templates + RTTI + shared library = impossible? - comp.lang.c++ ..... dynamic_cast<>, and I'd ... queries in the resolver ? hi, recently i have been bitten by a problem ... Making use of run-time dynamic linking (Examples?) - comp.lang.c++ ...... programming interface to dynamically load a shared library ... This is to avoid the problem that you don't control ... member functions in classes derived from templates ... GCC Frequently Asked Questions - GNU Project - Free Software ...This problem manifests itself by programs not finding shared libraries they depend on when the programs are ... the above precautions, you may discover that a template ... Shared library issues - Open Standards... systems support dynamic shared libraries ... programs that use dynamic libraries. We don't have to solve all problems ... specializations of Standard Library templates ... 7/11/2012 8:10:49 PM
|
|
|
|
|
|
|
|
|