C++ header-only template library

2019-01-19 00:10发布

Looking at this project (http://www.savarese.com/software/libssrckdtree/) I found the definition "C++ header-only template library". At the moment I have basic C++ knowledge but would like to know what this exactly means and why this people use it on this project

9条回答
干净又极端
2楼-- · 2019-01-19 00:45

Some libraries take the form of a binary file you must link with your project, along with a header file that defines the available classes or functions. A "header-only library" would be one that includes no binary file, just a header you include in your source.

Templates are classes or functions that are customized to their particular use; they're typically defined in a header file since the compiler must read their source to customize them. You can't compile a template to a binary file until you know exactly how it's going to be used, so you include the source along with your own code and the compiler can then process them together.

查看更多
Viruses.
3楼-- · 2019-01-19 00:46

It means that all of the code is in header files; there are no libraries associated with the library. What that means in practice depends—in the worst case, it means that the author has never even compiled the code:-). Most likely, it means that the code has never been tested with the exact combination of compiler, version and options that you use, and that compile times will shoot way up. On the other hand, it means that you can use the library even if the author doesn't have access to the same compiler as you, and you're not forced to use whatever options he used when he compiled the library. Or alternatively, if it is open source, you don't have to build the library yourself.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-19 00:53

For template libraries, it's possible to provide all of the functionality in just header (.h files) because traditionally compilers needed the full definition of the template class in order to instantiate for a given type. There is nothing to put in a library unless the library is going to provide pre-instantiated versions or if there is some portion of the template library that doesn't need to be templated.

查看更多
登录 后发表回答