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.
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.