Obviously template libraries need to be header only, but for non-templates, when should you make things header-only?
Obviously template libraries need to be header only, but for non-templates, when should you
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you think your non-template library could be header-only, consider dividing it into two files anyway, then providing a third file that includes both the
.hand the.cpp(with an include guard).Then anyone who uses your library in a lot of different TUs, and suspects that this might be costing a lot of compile time, can easily make the change to test it.
Once you know users have the option which way to use the library, the answer probably becomes “offer that option whenever you possibly can”. So pretty much any time that including it from multiple TUs wouldn’t violate the ODR. For instance, if your non-
staticfree functions refer tostaticglobals, then you’re out of luck, since the different definitions of that function in different TUs would refer to different objects by the same name, which is an ODR-violation.