Enable usage of gsl::narrow with exceptions disabled#640
Enable usage of gsl::narrow with exceptions disabled#640annagrin merged 3 commits intomicrosoft:masterfrom
Conversation
include/gsl/gsl_assert
Outdated
| // Temporary until MSVC STL supports no-exceptions mode. | ||
| // Currently terminate is a no-op in this mode, so we add termination behavior back | ||
| // | ||
| #if defined(_MSC_VER) && !_HAS_EXCEPTIONS |
There was a problem hiding this comment.
I think you want to test if _HAS_EXCEPTIONS is #defined before testing its value. People catch me making that mistake all the time.
tests/no_exception_tests.cpp
Outdated
| #include <gsl/gsl_util> // for narrow_cast, at | ||
| #include <gsl/span> // for span, span_iterator, operator==, operator!= | ||
|
|
||
| #include "test_noexcept.hpp" |
There was a problem hiding this comment.
Everywhere else we use .h I think. Any reason to not follow suit here and have test_noexcept.h?
There was a problem hiding this comment.
copied from catch.hpp, but no reason to do it here
tests/test_noexcept.cpp
Outdated
| /////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #include "test_noexcept.hpp" | ||
| int main() |
There was a problem hiding this comment.
Wouldn't you want to install a replacement to the usual "terminate" behavior in this test so you can ensure each case ends up at the terminate-handler as expected without having to actually kill the process?
There was a problem hiding this comment.
I am not using the catch infrastructure, need to think about the best way to implement this, so will resolve it in a later PR
7d8d1a7 to
a14aeef
Compare
|
Addressed #if-related and .hpp comments. Will implement testing for no-exception mode separately. |
|
LGTM, thanks @annagrin! |
This solution uses the approach of boost::asio to enabling usage of the library in environments where exception usage is either prohibited or not feasible (due to code size constraints). A function template gsl::throw_exception has been added, which in a normal environment just throws the exception. However, when GSL_TERMINATE_ON_CONTRACT_VIOLATION is defined the function is only declared by gsl and the definition of this function template must be supplied by the library's user. Closes: microsoft#468 Signed-off-by: Damian Jarek <damian.jarek93@gmail.com> Addition: - understand STL no exception macro - use function static variable to set termination handler in kernel mode - add compile-only tests for no-exception mode
f9236a7 to
4c49c9e
Compare
|
With this latest change I get a warning from VS 15.6.2 core guideline code analysis (I'ld prefer not to see warnings from GSL): gsl\include\gsl\gsl_assert(106): warning C26440: Function 'gsl::details::throw_exceptiongsl::narrowing_error' can be declared 'noexcept' (f.6: http://go.microsoft.com/fwlink/?linkid=853927). We could add noexcept but then other warnings come up ("gsl::narrow can be declared noexcept"). So I suggest suppressing the warning. What do you think? Shall I prepare a PR? |
This change is based on the previous PR by @djarek:
This solution uses the approach of boost::asio to enabling usage of the
library in environments where exception usage is either prohibited
or not feasible (due to code size constraints). A function template
gsl::throw_exception has been added, which in a normal environment just
throws the exception. However, when GSL_TERMINATE_ON_CONTRACT_VIOLATION
is defined the function is only declared by gsl and the definition of
this function template must be supplied by the library's user.
Closes: #468
Signed-off-by: Damian Jarek damian.jarek93@gmail.com
Addition:
mode, and throws in all others
- understand MSVC STL no exception macro
- use function static variable to set termination handler in no-exception mode (msvc)