The output from std :: exception in the library: does the header-only solution work for throwing exceptions?

In our cross-platform Open Source library, we get from std :: exception to define custom exceptions that can be caught in library code as well as in user code. I saw that this is really the recommended procedure, but in Visual Studio 2015 (or rather, in the new version of MSVC) a warning is added to the implementation class ( warning C4275 ) - see also here: How is the dllexport class derived from std :: runtime_error?

Of course, we could just ignore the mistake, but it seems wrong to me.

The reason for the warning compared with older versions of Visual Studio, apparently, is that std :: exception is used for export in an older version of MSVC, but so far it is no longer exported. In any case, I feel that this was never the “right way” to continue in the form of compiling it into a library.

From what I read in the answers, the best way to do this is to “embed” the class, since exporting the base class (std :: exception) can lead to more complications. If I understand correctly, "inlining" here means not using the keyword "inline", but moving the definition to the header and not exporting it. Is that even right?

Assuming this is what is meant: my question is that a compiled library that throws exceptions that are defined in one of its headers will allow you to correctly catch exceptions in the executable, dynamically linking this library. But what if the compilers are different? Runtime type information (RTTI) seems relevant here, so is it guaranteed to work in this way even when using different versions of the compiler or even different compilers? If this does not work, how to solve it in the “right” way?

+5
source share
1 answer

Quote from Microsoft Connect ( webarchive ) issue in the message related in the question;

... placing STL types in your DLL makes you play according to STL rules (in particular, you cannot mix different major versions of VC, and your IDL parameters must match). However, there is a workaround. The C4251 is essentially noise and can be turned off ...

Mixing versions and compiler options could (and probably at some point) cause problems and unpredictable application behavior. Thus, there are no guarantees that it will work, perhaps, on the contrary, it will not work.

Using the built-in method described above (i.e., implementing only the header) and ensuring the consistency of parameters and compilers within the framework of projects, it allows working with dll export restrictions.

Given that this is an open source project, it can be easily created for the client environment, so any of the mentioned methods should be suitable, because the client will be able to create the code taking into account their compiler and the parameters that they desire.

+6
source

All Articles