Reputation: 1197
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
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:
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
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:
template<typename X>
void foo(X x);
template<>
void foo<int>(int x);
#include "header.h"
template<>
void foo<int>(int x)
{
}
#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
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
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