Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

static library compatibilty for multiple c++ versions

I’m making a library that supports multiple c++ versions (14/17/20) using the __cplusplus macro. For example I’d like to use the nodiscard and optional from cpp20 but still be compatible with cpp14.
Obviously when compiling it to a .lib the c++ version will be set to the original version and not the version of the project using the library.

So my question regarding the best approach

  • should I create multiple .lib files for the different c++ versions
  • or more sensibly, should I create a header only library

The second option is naturally more appealing, yet I find the combination declaration + implementation in the same file a little verbose.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Are there any other ways of handling this?
Would a DLL be better in this case?

>Solution :

nodiscard and std::optional are C++17 not C++20 as you stated. I would seriously suggest that you drop support for C++14 and move on with your life. But if you can’t do that, you can make your own wrapper macro for nodiscard which uses the older approaches where required: Ways to specify [[nodiscard]] before C++17

For std::optional if you need C++14 support, just don’t use std::optional in your API, keep it as an internal implementation detail. It’s very likely that std::optional is a header-only feature on the systems where you build, so as long as it’s not part of your public API your users probably won’t notice.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading