softghost
softghost

Reputation: 1197

How to create a commercial library with template using in 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!

Upvotes: 2

Views: 985

Answers (4)

Dietmar Kühl
Dietmar Kühl

Reputation: 153840

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..,

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258618

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.

Upvotes: 1

bames53
bames53

Reputation: 88175

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
}

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385204

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.

Upvotes: 3

Related Questions