467,920 Members | 1,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

Problems with Templates with Shared libraries

AH
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
Jul 19 '05 #1
  • viewed: 3631
Share:
1 Reply
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.

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rickard Lind | last post: by
13 posts views Thread by SpaceCowboy | last post: by
15 posts views Thread by Rob Ratcliff | last post: by
1 post views Thread by Leslaw Bieniasz | last post: by
5 posts views Thread by Christian Christmann | last post: by
reply views Thread by ZR | last post: by
5 posts views Thread by David T. Ashley | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.