C++ header-only template library

后端 未结 9 2119
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 20:21

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++ knowled

9条回答
  •  猫巷女王i
    2020-12-09 20:53

    It means all the definitions of template (function template or class template) are in the headers only. There is no .cpp file. There are only .h files (or some other extensions such as .hpp or no extension at all like , string> etc)

    C++ compilers require the definitions of templates to be present in the same file in which they're declared. As such, the header-only library is neither static library or dynamic library. Its source-code library which means you can see the implementation in the headers. You've include the header files in your code, which gets compiled along with the headers from the library.

    Note the part of the C++ Standard Library which makes use of templates such as , string>, , etc is header-only library.

    Actually templates (class templates and function templates) cannot be compiled into static or dynamic library to be linked to programs. A template is, as the term itself says, a template; it's not normal code; its only when you use it in your code passing template argument(s) (which is either type or value), the compiler generates a compilable function/class out of the function/class template:

    template
    struct A
    {
       T data;
    };
    
    struct B
    {
       int data;
    };
    

    Here, A cannot be compiled into binary (static library or dynamic library), because the compiler doesn't know what T is. But B can be compiled into binary, as the compiler has complete information about it.

    So you can read the phrase "class template A" as : A is a template for a class. A itself is not a class. But B is a class, its not a template.

    As the class template A cannot be compiled into static or dynamic library to be linked to your programs, so A can be shipped only as header-only library with full source code. Likewise

提交回复
热议问题