header-only

How do I create a header-only library?

百般思念 提交于 2019-11-29 19:52:40
I'd like to package a library I'm working on as a header-only library to make it easier for clients to use. (It's small and there's really no reason to put it into a separate translation unit) However, I cannot simply put my code in headers because this violates C++'s one definition rule. (Assuming that the library header is included in multiple translation units of a client project) How does one modify a library to make it header-only? You can use the inline keyword: // header.hpp (included into multiple translation units) void foo_bad() {} // multiple definitions, one in every translation

How do I create a header-only library?

笑着哭i 提交于 2019-11-28 15:38:43
问题 I'd like to package a library I'm working on as a header-only library to make it easier for clients to use. (It's small and there's really no reason to put it into a separate translation unit) However, I cannot simply put my code in headers because this violates C++'s one definition rule. (Assuming that the library header is included in multiple translation units of a client project) How does one modify a library to make it header-only? 回答1: You can use the inline keyword: // header.hpp

C++ header-only template library

泄露秘密 提交于 2019-11-28 10:03:12
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 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 <vector> , string> etc) C++ compilers require the definitions of templates to be present in the same file in which

C++ header-only template library

本小妞迷上赌 提交于 2019-11-27 03:23:26
问题 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 回答1: 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 <vector> ,

How to have static data members in a header-only library?

*爱你&永不变心* 提交于 2019-11-27 01:55:24
What is the best way to have a static member in a non-templated library class, without placing the burden of defining the member on the class user? Say I want to provide this class: class i_want_a_static_member { static expensive_resource static_resource_; public: void foo() { static_resource_.bar(); } }; Then the user of the class must not forget to define the static member somewhere (as already answered many times ): // this must be done somewhere in a translation unit expensive_resource i_want_a_static_member::static_resource_; I do have an answer below, but it has some disadvantages. Are

Benefits of header-only libraries

☆樱花仙子☆ 提交于 2019-11-26 23:48:27
What are the benefits of a header only library and why would you write it that way oppose to putting the implementation into separate file? There are situations when a header-only library is the only option, for example when dealing with templates. Having a header-only library also means you don't have to worry about different platforms where the library might be used. When you separate the implementation, you usually do so to hide implementation details, and distribute the library as a combination of headers and libraries ( lib , dll 's or .so files). These of course have to be compiled for

How to have static data members in a header-only library?

时光怂恿深爱的人放手 提交于 2019-11-26 09:46:36
问题 What is the best way to have a static member in a non-templated library class, without placing the burden of defining the member on the class user? Say I want to provide this class: class i_want_a_static_member { static expensive_resource static_resource_; public: void foo() { static_resource_.bar(); } }; Then the user of the class must not forget to define the static member somewhere (as already answered many times): // this must be done somewhere in a translation unit expensive_resource i

Benefits of header-only libraries

余生颓废 提交于 2019-11-26 08:47:07
问题 What are the benefits of a header only library and why would you write it that way oppose to putting the implementation into separate file? 回答1: There are situations when a header-only library is the only option, for example when dealing with templates. Having a header-only library also means you don't have to worry about different platforms where the library might be used. When you separate the implementation, you usually do so to hide implementation details, and distribute the library as a