写一个C或C ++与&QUOT库;模板" [英] write a C or C++ library with "template"

查看:128
本文介绍了写一个C或C ++与&QUOT库;模板"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(1)。当使用C ++模板,它是正确的,因为编译器(如G ++),将无法编译模板定义(只能​​在头文件中没有源文件)直接,但基于对每个模板定义生成code的实例,然后编译生成的code为它的实例?

(2)。如果我想写一个C ++库,它提供了模板类和模板函数,是不可能编译库到共享文件(的.so,.a)中,因为他们的实例不会在图书馆的code在任何​​地方,但只出现在用户的程序?如果是的话,岂不是模板库,只是源$ C ​​$ C文件没有precompiled文件?

如何C ++标准模板库(STL)实现的?是它的源代码code precompiled或者与用户程序一起编译?

(3)。在C,

如何编写提供的功能作用类似于C ++模板函数库?超载是一个很好的解决方案?

如果我必须写一个程序转化为不同类型的参数不同的功能,是有code再利用的好方法?这是一个好办法做到这一点 http://www.vlfeat.org/api/imop_8c_source.html 的?其他方法吗?

感谢和问候!


解决方案

  

在使用C ++模板,它是正确的,因为编译器(例如G ++)
  不会编译模板
  定义。


是的。这是一个正确的假设。

一个模板定义是​​不完整的code。你需要在编译之前将填写模板参数。


  

如果我想写一个C ++库,它提供了模板类和
  模板功能,是不可能
  编译库到共享
  文件(的.so,.a)中


没有这是不可能的。你可以只编译模板实例个体


  

如何为C ++标准模板库
  (STL)实现的?是它的源代码code
  precompiled或编在一起
  用户的程序?


在STL code的很大一部分驻留在头文件和获取与您的应用程序一起编译。


  

在C,如何编写一个库,
  提供像个模板函数
  在C ++函数?这是一个好办法
  去做吧
  <一href=\"http://www.vlfeat.org/api/imop_8c_source.html\">http://www.vlfeat.org/api/imop_8c_source.html?
  其他方法吗?


重新定义包括一个宏(如你所提供的链接证明)后,同一个文件多次是做到这一点的好办法。

(1). When using C++ template, is it correct that the compiler (e.g. g++) will not compile the template definition (which can only be in header file not source file) directly, but generate the code based on template definition for each of its instantiations and then compile the generated code for its instantiations?

(2). If I want to write a C++ library which provide template classes and template functions, is it impossible to compile the library into shared file (.so, .a) because their instantiations will not be anywhere in the code of the library but only appear in the user's program? If yes, does it mean that template libraries are just source code files not precompiled files?

How is C++ standard template library (STL) implemented? Is its source code precompiled or compiled together with user's program?

(3). In C,

how to write a library that provide functions acting like template functions in C++? Is overloading a good solution?

If I have to write a procedure into a different function for different types of arguments, is there a good way for code reusing? Is this a good way to do it http://www.vlfeat.org/api/imop_8c_source.html? Any other ways?

Thanks and regards!

解决方案

When using C++ template, is it correct that the compiler (e.g. g++) will not compile the template definition.

Yes. It's a correct assumption.

A template definition is incomplete code. You need to fill in the template parameters before compiling it.

If I want to write a C++ library which provide template classes and template functions, is it impossible to compile the library into shared file (.so, .a)

No it's not possible. You can only compile individual instantiations of a template.

How is C++ standard template library (STL) implemented? Is its source code precompiled or compiled together with user's program?

A large part of the STL code resides in header files and gets compiled together with your application.

In C, how to write a library that provide functions acting like template functions in C++? Is this a good way to do it http://www.vlfeat.org/api/imop_8c_source.html? Any other ways?

Including the same file multiple times after redefining a macro (as demonstrated in the link you provided) is a good way to do this.

这篇关于写一个C或C ++与&QUOT库;模板&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆