How to create a commercial library with template using in C++? - c++

I'm a noob in C++ meta programming and I understand that I must place implementation of class in header file for convention. But when you want to purchase an package you need to separate the header and the implementation.
Now, Is there a way to do this?
Note: I know about 'export' keyword and read this page!

No, not really. Templates must be visible at compile-time so, if your users are to instantiate templates themselves, they need the definitions.
You'll just have to explicitly instantiate for as many template-parameter-list combinations as you feel you'll need, and hope that your users don't need any more.
Think of templates as something that helps you inside a single project. This inherent limitation means that they are not appropriate as a "I can provide my library for the user to specialise with any type they want" mechanism.

There are several vendors providing general templates for purchase (BTW, I assume you want to sell the template code rather than purchase it) and they simply ship the source. The license agreement states that you can't distribute or modify the code. Done.
If that's not good enough for your needs here are a few things you can do:
Factor code into components with minimal dependency on template arguments. This may yield parts of the code you can put into a readily compiled object because they don't depend on template arguments or you can preinstantiate the code.
Obfuscating the code may be one way to go. In this context it is worth noting that a reason given for the absense of an "Obfuscated C++ Contest" by Steve Clamage was: "That would be like shooting fish in a barrel."
Create a service where customers upload the class declarations needed for template instantiation and get back the compiled instantiation. Of course, if you don't trust the customers they have no reason to assume that you might be trustworthy.
Wait for the next revision of the standard which hopefully has some sort of module support helping with this issue. I wouldn't bet too high on this, however.
Personally, I wouldn't bother: from feedback I get back on my code it seems that despite documentation and explanations nobody is prepared to touch it. I think it is rather readable..,

templatelib.h
template<typename T> void foo();
templatelib.cpp
#include <iostream>
#include "templatelib.h"
template<typename T> void foo() {
std::cout << typeid(T).name() << '\n';
}
template void foo<int>(); // explicitly instantiate template for this type
main.cpp
#include "templatelib.h"
int main() {
foo<int>(); // Okay
foo<double>(); // error
}

Not really. You can obfuscate your code, you can separate the implementation in a impl file and include it in the header, but it must be visible to the compiler.
However, if there's a way you can restrict how many actual specializations are used, you can move specialized implementations in a cpp file, keep the declaration in the header, and it would still work:
header.h
template<typename X>
void foo(X x);
template<>
void foo<int>(int x);
impl1.cpp
#include "header.h"
template<>
void foo<int>(int x)
{
}
impl2.cpp
#include "header.h"
foo(1); //works
Also, I don't think you should worry about this. Because they are generic, I don't think templates can have that much of an important logic inside of them. Take a look at the standard ones - vector, list - sure, they would take some time to write from scratch, but there's no real commercial secret in there.

Related

Reduce sizes of template headers

I'm developing a C++ library that makes heavy uses of templates. For most of the classes, all the code resides in some .hpp file that will be #included directly by client code. There are two things that I'm worrying about:
Is there some design pattern for moving implementation code to the "background"? When I design non-template classes I often separate the "interface" class and the implementation class, and the former will delegate to the latter by doing something like getImpl()->foo(), so that implementation code can be dynamically linked to. I'm not sure how to do this for templates. Probably dynamic linking just doesn't make sense for template, I think?
Are big template headers (> 1000 lines) common? Or is that bad? If it's bad, what can I do with it?
I am aware of this question How to reduce output size of template-heavy C++ code?, but I think we are asking about different things: the OP of that question is trying to reduce the size of the output, while I am trying to reduce the size of my library headers themselves.
UPDATE: For example, if you were to design std::vector, how would you organize its interface & implementation (if needed)?
Don't worry too much about the header size. Templates almost always have all their code in a header because each client must know how instantiate the class in case they are creating a completely new version (with different template parameters).
If you're worried about 1000 lines, take a look at vector. In Visual Studio 2013, it's 3000 lines.
Having said that, you really only need the declaration of classes available everywhere and the definition once, like any normal class. So it is possible to have a template header with no implementation and then create a .cpp with the implementations. This .cpp will have to force the generation of all the templated types that you need everywhere.
In the example below, the author of B.cpp must know all template parameters that will ever be used. You don't need this many files, but I think this should cover every scenario.
B.h:
template<class T>
struct B
{
B();
};
B_impl.h:
#include "B.h"
template<class T>
B<T>::B() {}
B.cpp:
#include "B_impl.h"
template struct B<int>;
template struct B<short>;
main.cpp:
#include "B.h"
B<int> b;

Strategy for wrapping multiple libraries in C++

I have a class Foo, which I do not implement directly, but wrap external libraries (e.g FooXternal1 or FooXternal2 )
One way that I have seen to do this, is using preprocessor directives as
#include "config.h"
#include "foo.h"
#ifdef _FOOXTERNAL1_WRAPPER_
//implementation of class Foo using FooXternal1
#endif
#ifdef _FOOXTERNAL2_WRAPPER_
//implementation of class Foo using FooXternal2
#endif
and a config.h is used to define these preprocessor flags (_FOOXTERNAL1_WRAPPER_ and _FOOEXTERNAL2_WRAPPER_).
I have the impression this is frowned upon by the C++ programmer community because it uses preprocessor directives, is hard to debug, etc. Further, it does not allow for the parallel existence of both implementations.
I thought about making Foo a base class and inheriting from it to allow for both implementations to exist in parallel with each other. But I ran into two problems:
Pure virtual functions: cannot instatiate an object of type 'Foo', which I need during use.
Virtual functions run the risk of running an object with no (proper) implementation.
Am I missing something? Is there a cleaner way to do this?
EDIT : To summarize, there are 3(.5?!) ways to doing the wrapping- 2(.5) are given by icepack, and the last by Sergey
1- Use factory methods
2- Use preprocessor directives
2.5- Use makefile or IDE to effectively do the work of the preprocessor directives
3.5- Use templates suggested by Sergay
I am working on an embedded system where resources are limited, I decided to use template<enum = default_library>, with template specialization. It is easy to understand for later users; at least thats what I think
If all method names of external implementations are similar, you can use templates. Let external implementations look like:
class FooX1
{
public:
void Met1()
{
std::cout << "X1\n";
}
};
class FooX2
{
public:
void Met1()
{
std::cout << "X2\n";
}
};
Then you can use several variants.
Variant 1. You can declare member of a template type and wrap all calls to external implementation, even with some preparations before the call. Don't forget to delete impl in ~Foo destructor.
template<typename FooX>
class FooVariant1
{
public:
FooVariant1()
{
impl=new FooX();
}
void Met1Wrapper()
{
impl->Met1();
}
private:
FooX *impl;
};
Usage:
FooVariant1<FooX1> bar;
bar.Met1Wrapper();
Variant 2. You can inherit from a template parameter. In this case you don't declare any members, but just call implementation's methods by their names.
template<typename FooX>
class FooVariant2 : public FooX
{
};
Usage:
FooVariant2<FooX1> bar;
bar.Met1();
A disadvantage of using templates is that there is no easy way to change implementations in runtime. But in return you get much more optimal code, because types are generated in compile-time and there is no table of virtual functions, which can make the program slower.
If you want the 2 implementations to coexist at runtime, interface is the way to go (for example, you can use a factory method design pattern to instantiate the concrete object, like #n.m. has suggested).
If you can decide at compilation time what is the implementation that you need, you have several options:
Still use interface. This will allow an easy transition if in the future you'll need both implementations at runtime.
Use preprocessor directives. There is nothing wrong here as far as C++ is considered. It's a pure design issue.
Put the implementations in different files and configure your compiler to compile either one of them according to settings - this is actually similar to using preprocessor directives but it's cleaner and doesn't add garbage to your code (since the flags are in the solution/makefile/whatever your compiler uses).
The only thing I'd frown upon is including both implementations in the same source file. That might get confusing. Otherwise, this is one of the things preprocessor flags are good at, especially if you're not linking both libraries at the same time. It's just like supporting multiple operating systems. Provide a consistent interface in all cases and hide the implementation details somewhere else.
Does type Foo need to hold any information specific to each library? If not, you might be able to get away with this:
#include "Foo.h"
#if defined _FOOXTERNAL1_WRAPPER_
#include "Foo_impl1.cpp"
#elif defined _FOOXTERNAL2_WRAPPER_
#include "Foo_impl2.cpp"
#else
#error "Warn about a missing define here"
#endif
This way you don't have to bother with virtual functions or inheritance and you still prevent any member functions from going unimplemented.
Keep Foo abstract. Provide a factory method
Foo* MakeFoo();
that allocates a new object of either type FooImpl1 or FooImpl2, and returns its address.
Wikipedia on Factory Method pattern.

Declaration and Implementation of Functions

According to my teacher, it's bad practice to write user-defined functions like this:
int DoubleNumber(int Number)
{
return Number * 2;
}
int main()
{
cout << DoubleNumber(8);
}
Instead, he says to always use forward declarations, even if the functions don't need any knowledge of each other:
int DoubleNumber(int Number); // Forward declaration.
int main()
{
cout << DoubleNumber(8);
}
int DoubleNumber(int Number) // Implementation.
{
return Number * 2;
}
I find this especially strange since he made a point of telling us how important it is that the forward declaration and implementation are exactly the same or you'll get errors. If it's such a big deal, why not just put it all above main()?
So, is it really bad practice to declare and implement at the same time? Does it even matter?
If you don't declare the forward declarations ("prototypes"), then you need to make sure that all your functions occur before any functions that depend on them, i.e. in the reverse order of the call graph. That's fine for a simple example as above, but is a complete pain for anything more realistic (and in some cases impossible, if there are any loops in the call graph).
I think your teacher is an old C programmer.
If you wrote a C program without forward declarations and one function called another function declared later in the file (or in a different compilation unit), the compiler would not complain but silently pretend to know what the prototype should be.
Debugging is horrible, if you don't know if your compiler is passing the arguments correctly. Therefore it was a good defensive policy to always declare all functions; at least the compiler could raise an error if the declaration did not match the implementation.
C compilers and tool have gotten better (I hope). It is still not an error to call an unknown function, but GCC for example is kind enough to warn by default.
But in C++ you can't call a function that hasn't been declared or defined. Consequently, C++ programmers don't worry much about forward declarations.
Your teacher's policy is horrible IMHO. Use forward declarations only when they're really needed. That way, their presence demonstrates their necessity, which gives the reader useful documentation (i.e., there may be mutual recursion between the functions). Of course you do need forward declarations in header files; that's what they're for.
In my first programming class, the teacher also emphasized this point. I'm not exactly sure there is a benefit to such a simple case in actual software.
However, it does prepare you for using header files if you haven't covered that yet. In a typical case, you will have a header file custom-math.h and source file custom-math.cpp where custom-math.h contains the forward declaration and custom-math.cpp the implementation. Doing so may increase compilation time significantly when doing modifications to function implementations only in large projects. It is also a convenient way to split your program into "logical" groups of functions and/or classes.
If you are going to put other functions in the same file as main(), then what you do probably depends on your personal preference. Some people prefer to have main() close to the top to get to the program logic right away. In this case, forward declare your functions.
Karl Knecthel writes "Use forward declarations only when they're really needed. That way, their presence demonstrates their necessity, which gives the reader useful documentation (i.e., there may be mutual recursion between the functions)." and IMHO that's sound advice.
Oli Charlesworth talks about "complete pain" for ordering functions so that they can be called without forward declarations. That's not my experience, and I cannot imagine how that pain/problem is accomplished. I suspect a PEBCAK problem there.
A practice of using forward declarations for all functions will not save you from PEBCAK problems, but they do introduce needless maintainance work and needless more code to relate to, and they do make it more unclear which functions really need forward declarations.
If you get to the point where forward declarations could help to see function signatures at a glance, when forced to some very simple editor, then there are two actions that should be taken: (1) refactoring of the code, and (2) switching to a better editor.
Cheers & hth.,
Ask your teacher about why he recommends this ;) Anyway, it's not bad practice in my opinion, and in most cases it doesn't even matter. The biggest advantage of declaring all functions upfront is that you have a high-level overview of what the code does.
Traditionally you'll be sticking all your prototypes in a header file, so they can be used by other source files - or at least, you'll put the ones you want to expose in the .h file.
As far as code where it's not necessary, there's something to be said for putting all your file-level declarations at the top (variables and functions) because it means you can move functions around at-will and not have to worry about it. Not to mention, I can see every function in a file right away. But something like:
void Func1() { ... }
...
void Func2() { ... }
...
void Func3() { ... }
...
int main() { Func1(); Func2(); Func3(); return 0; }
That - that is to say, a number of disjointed functions all called by main() - is a very common file, and it's perfectly reasonable to forgo the declaration.
Blanket rules are rarely correct. The public api you would normally expose through the prototypes in the header file. The rest of the functions will likely to be in an anonymous namespace in the cpp file. If those are called multiple times in the implementation it make sense to provide prototypes at the top, otherwise every function using them would have to provide prototypes before calling functions. At the same time if some function is used multiple times in the cpp file it might be an indication that it's universal enough to be moved to a common api. If the functions are not used all over the place, it's better to provide as limited exposure to them as possible, i.e. declaring and defining them close to the place they are called from.
Personally, I like to only forward declare anything that client code needs to know about (i.e. in a class header). Anything that's local to a class implementation (such as helper functions), should be defined prior to usage.
Of course at the end of the day, for the most part, it boils down to either personal preference or coding standard that your project is following.

Designing a C++ library

I am in the process of designing a C++ static library.
I want to make the classes generic/configuarable so that they can support a number of data types(and I don't want to write any data type specific code in my library).
So I have templatized the classes.
But since the C++ "export" template feature is not supported by the compiler I am currently using, I am forced to provide the implementation of the classes in the header file.
I dont want to expose the implementation details of my Classes to the client code which is going to use my library.
Can you please provide me with some design alternatives to the above problem??
Prior to templates, type-agnostic C++ code had to be written using runtime polymorphism. But with templates as well, you can combine the two techniques.
For example, suppose you wanted to store values of any type, for later retrieval. Without templates, you'd have to do this:
struct PrintableThing
{
// declare abstract operations needed on the type
virtual void print(std::ostream &os) = 0;
// polymorphic base class needs virtual destructor
virtual ~PrintableThing() {}
};
class PrintableContainer
{
PrintableThing *printableThing;
public:
// various other secret stuff
void store(PrintableThing *p);
};
The user of this library would have to write their own derived version of PrintableThing by hand to wrap around their own data and implement the print function on it.
But you can wrap a template-based layer around such a system:
template <T>
struct PrintableType : PrintableThing
{
T instance;
virtual void print(std::ostream &os)
{ os << instance; }
PrintableType(const T &i)
: instance(i) {}
};
And also add a method in the header of the library, in the declaration of the PrintableContainer class:
template <class T>
void store(const T &p)
{
store(new PrintableType(p));
}
This acts as the bridge between templates and runtime polymorphism, compile-time binding to the << operator to implement print, and to the copy-constructor also (and of course also forwarding to the nested instance's destructor).
In this way, you can write a library entirely based on runtime polymorphism, with the implementation capable of being hidden away in the source of the library, but with a little bit of template "sugar" added to make it convenient to use.
Whether this is worth the trouble will depend on your needs. It has a purely technical benefit in that runtime polymorphism is sometimes exactly what you need, in itself. On the downside, you will undoubtedly reduce the compiler's ability to inline effectively. On the upside, your compile times and binary code bloat may go down.
Examples are std::tr1::function and boost::any, which have a very clean, modern C++ template-based front end but work behind the scenes as runtime polymorphic containers.
I've got some news for you, buddy. Even with export, you'd still have to release all of your template code -- export just makes it that you don't have to put the definitions in a header file. You're totally stuck. The only technique you can use is split off some functions that are non-templates and put them into a different class. But that's ugly, and usually involves void* and placement new and delete. That's just the nature of the beast.
You can try to obfuscate your code - but you have little choice in C++03 asides from including template code in header files.
Vandevoorde does describe another technique in his book: Explicit instantiation - but that entails having to explicitly instantiate all possible useful combinations.
But for the most comprehensive review of this topic read chapter 6 from C++ Templates: The Complete Guide.
Edit (in response to your comment): You have two options for writing generic code without using templates:
1) Preprocessor - still requires header files
2) using void* - yuk - incredibly unsafe
So no, i do not recommend not using templates for solving problems that templates were specifically designed (albeit somewhat flawed) for.
One problem with templates is that they require compiled code. You never know how the end-user will specialize/instantiate your templates, so your dll-file would have to contain all possible template specializations in compiled form. This means that to export pair<X,Y> template you would have to force the compilication of pair<int,float>, pair<int,string>, pair<string,HWND> and so on... to infinity..
I guess more practical solution for you would be to un-template private/hidden code. You can create special internal functions that would only be compiled for single template specialization. In the following example internal_foo-function is never called from MyClass where A is not int.
template<class A>
class MyClass
{
int a;
float b;
A c;
int foo(string param1);
{
((MyClass<int>*)this)->internal_foo(param1);
}
int internal_foo(string param1); // only called on MyClass<int> instances
};
template<>
__declspec(dllexport) int MyClass<int>::internal_foo(string param1)
{
... secret code ...
}
This of course is a hack. When using it you should be extra careful not to use member variable "c", because it's not always integer (even though internal_foo thinks that it is). And you can't even guard yourself with assertions. C++ allows you to shoot yourself in the foot, and gives you no indications about it until it's too late.
PS. I haven't tested this code so it might require some fine tuning. Not sure for example if __declspec(dllimport) is needed in order for compiler to find internal_foo function from dll-file...
With templates you cannot avoid shipping the code (unless your code only works with a fixed set of types, in which case you can explicitly instantiate). Where I work we have a library that must work on POD types (CORBA/DDS/HLA data definitions), so at the end we ship templates.
The templates delegate most of the code to non-templated code that is shipped in binary form. In some cases, work must be done directly in the types that were passed to the template, and cannot thus be delegated to non-templated code, so it is not a perfect solution, but it hides enough part of the code to make our CEO happy (the people in charge of the project would gladly provide all the code in templates).
As Neil points in a comment to the question, in the vast majority of cases there is nothing magical in the code that could not be rewritten by others.

How do I force a particular instance of a C++ template to instantiate?

See title. I have a template. I want to force a particular instance of a template to instantiate. How do I do this?
More specifically, can you force an abstract template class to instantiate?
I might elaborate as I have the same question. In my case I am building a library, some of the template implementations are large and include lots of stuff, but are only generated for a couple of types. I want to compile them in the library and export all the methods, but not include the header with the code everywhere.
ie:
template<class T>
OS_EXPORT_DECL class MyTmpl
{
T *item1;
public:
inline T *simpleGetT() { return(item1); } /* small inline code in here */ }
T *doSomeReallyBigMergeStuff(T *b); // note only declaration here
};
// *** implementation source file only seen inside library
template<class T>
MyTmpl<T>::doSomeReallyBigMergeStuff(T *b)
{
... a really big method, but don't want to duplicate it,
so it is a template ...
}
I could of course reference all the methods inside the library which would force them to compile and export but the desire isn't to add un-needed code to the library like the argument formatting for the items and the code to call them etc.
????? specifically I am building the library for several versions of MSC and GCC and intel compilers.
What you also can try is explicit instantiation:
template class vector<int>; // class
template int& vector<int>::operator[](int); // member
template int convert<int,double>(double); // function
You can't force generic templates to instantiate, the compiler can only generate code if the type is completely known.
Forcing an instantiation is done by providing all types explicitly:
template class std::vector<int>;
Comeaus template FAQ covers the related issues in some detail.
You can force instantiation by using the template with the desired parameter. For example you could define a function using all the required methods:
void force_int_instance() {
Abstract<int> *a;
a->some_method();
a->some_other_method(1, 2, 3);
}
You don't need to actually call that function anywhere, so it's not a problem that the pointer is not initialized. But the compiler has to assume that the function might be called from another object file, so it has to instantiate the template.
If I understand your question correctly, you have a template class, and you want to force the compiler to generate the code for use with some specific type. For example, you may want to ensure the code for std::vector<int> exists in your program.
The best way to ensure this is to simply construct an instance of the class:
void EnsureInstantiation()
{
std::vector<int> intvector;
std::vector<boo> boolvector;
/// etc.
}
The trick is that you don't even have to call EnsureInstantiation anywhere in your code. Just make sure it's not static or else the compiler may optimize it out.
abstract class cannot be instantiated.you probably want to do something along the lines of:
Abstract *a = new Implementation(...);
To force template instantiation, call template with template parameters:
std::max<int>(...);
std::pair<int, string>(...);
I'm going to answer what I think you meant, not what you said.
I am guessing the issue is one of two things. The first is that you have code in a template that's not getting compiled when you compile the template file itself, which can be very annoying. That can be fixed in your compiler settings.
The other is you want to have something special for a particular type, perhaps to debug it. That is called explicit instanciation but does not really instanciate anything just makes sure it's always defined after that point.
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/topic/com.ibm.vacpp6m.doc/language/ref/clrc16explicit_instantiation.htm