Problems with Templates with Shared libraries

  • Follow


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:













7/11/2012 8:10:49 PM


Reply: