由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 怎么隐藏c++template代码?只提供lib 不提供头文件
相关主题
C++ template problemC++ Q 103-105 (转载)
one template questionC++怎样设置全局变量
interview questions开源图片/文本分析
C++ linking 弱问 (one file)sgi stl 源代码一问
c function 在 c里调用和C++调用结果不一样template class instantiation问题
不用头文件,如何调用函数?C++问个template
recursive template?compile error
一个关于C++ template和overload的问题模板对象能不能作为成员变量使用
相关话题的讨论汇总
话题: c++话题: template话题: 头文件话题: 提供话题: lib
进入Programming版参与讨论
1 (共1页)
N******K
发帖数: 10202
1
核心库是c++ template写的 打算隐藏起来 只提供一些接口
怎么搞?
k**********g
发帖数: 989
2

Very difficult, and it will defeat some of the advantages of using C++
templates.
First of all, there is no such thing as shipping a binary or encoded version
of C++ template code without being "instantiated".
To instantiate, you need to provide all of the template parameters. In other
words, you need to know exactly what types will be needed by your customers.
http://en.cppreference.com/w/cpp/language/function_template#Exp
http://en.cppreference.com/w/cpp/language/template_specializati
Once that is done, you can use a combination of enums and function pointers
(and also C++ interfaces / abstract classes) to provide a de-genericized API.
Very difficult, but OpenCV is able to achieve something similar. The "de-
genericize" layer will contain a lots of enum checking and dispatching.
Also read this one: although I am not sure how relevant it is. https://www.
cisuc.uc.pt/publication/show/3134
Also read this one (generate machine code from C++ code) https://code.google
.com/p/jitasm/

【在 N******K 的大作中提到】
: 核心库是c++ template写的 打算隐藏起来 只提供一些接口
: 怎么搞?

b*******s
发帖数: 5216
3
instantiation + 头文件里extern声明

【在 N******K 的大作中提到】
: 核心库是c++ template写的 打算隐藏起来 只提供一些接口
: 怎么搞?

d****i
发帖数: 4809
4
不提供头文件不行吧,头文件相当于接口,没有接口人家怎么用你的库?你可以在你的
cpp文件里面实现你的类,然后在最后加上instantiation,比如
template class MyClass;
template class MyClass;
template class MyClass;
...
这样你只需提供lib和相应的头文件,而把你的实现代码隐藏

【在 N******K 的大作中提到】
: 核心库是c++ template写的 打算隐藏起来 只提供一些接口
: 怎么搞?

m********5
发帖数: 17667
5
很不方便
以前搞过,
大致就是Explicit Instantiation,每个可能用到的type编一遍
非常不practical
一般来说 C++ template lib 必开源
这是C++最大的问题之一,而不用template,用C++就很痛苦
但C++11 有 extern template,解决了这个问题

【在 N******K 的大作中提到】
: 核心库是c++ template写的 打算隐藏起来 只提供一些接口
: 怎么搞?

1 (共1页)
进入Programming版参与讨论
相关主题
模板对象能不能作为成员变量使用c function 在 c里调用和C++调用结果不一样
求助调试c++程序不用头文件,如何调用函数?C++
[菜鸟问题]类模板问题recursive template?
请问const myClass &src 和myClass const &src有什么区别?一个关于C++ template和overload的问题
C++ template problemC++ Q 103-105 (转载)
one template questionC++怎样设置全局变量
interview questions开源图片/文本分析
C++ linking 弱问 (one file)sgi stl 源代码一问
相关话题的讨论汇总
话题: c++话题: template话题: 头文件话题: 提供话题: lib