0

I have a library where I have a template class which takes a struct type as template argument and does some operations like save(dump in file), open(open a file to save the struct data) etc.

template<class T>
class MyDataSaver{};

so the users of the library create their structs with any members they wish and then write size, serialize like simple functions in struct which the template class uses and does the operations. creating new instance of template class

struct StructType
{
  int i;
  char ch;

  int size() { return sizeof(i) + sizeof(ch);}
  template<class Archive>
  void serialize(Archive ar){ar & i & ch;}
};

MyDataSaver<StructType> myData;
// And then
myData.save()
// and
myData.open().

Now my question, I want to convert the library to a dll. How can I restructure this code or create a new class, which can allow the library users to use the dll instead of the library with minimal change in code at library users end?

One crude approach I can think of is, create a new template class and have a byte array in it and export this and some how be able to store the struct type through this new class.

I want a dynamically linked dll(not those dlls which link with applications using lib files) which application can load. So if I make a change in dll, like change the data save media, or use crt file functions rather than OS specific one, the app using the dll should still be able to load and use it as if nothing was changed. So all changes, OTHER than changing the interface should have no impact on application.

Anand
  • 19
  • 6

1 Answers1

0

When you use templates, the compiler instantiates(create the types/classes) in compilation time. Hence you will need to know wich type will be T in order to instantiate a specific class in your dll, which as you sure are guessing is impossible.

One good approach is create header only libraries (like most of Boost libraries). And provide your API in *.hpp files.

If you don't want others see your code, well there are many ways to achive that, but that it's another story.

You can anlso be interested in reading this: Benefits of header-only libraries

Community
  • 1
  • 1
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60