By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Problems with Templates with Shared libraries

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.