mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Make narrow constexpr (#698)
This commit is contained in:
parent
9ff6e19ea9
commit
6eeed739f1
@ -37,6 +37,12 @@
|
|||||||
#endif // _MSC_VER < 1910
|
#endif // _MSC_VER < 1910
|
||||||
#endif // _MSC_VER
|
#endif // _MSC_VER
|
||||||
|
|
||||||
|
#if (defined(_MSC_VER) && _MSC_VER < 1910) || (!defined(__clang__) && defined(__GNUC__) && __GUNC__ < 6)
|
||||||
|
#define GSL_CONSTEXPR_NARROW 0
|
||||||
|
#else
|
||||||
|
#define GSL_CONSTEXPR_NARROW 1
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace gsl
|
namespace gsl
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
@ -111,6 +117,9 @@ namespace details
|
|||||||
template <class T, class U>
|
template <class T, class U>
|
||||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||||
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
|
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
|
||||||
|
#if GSL_CONSTEXPR_NARROW
|
||||||
|
constexpr
|
||||||
|
#endif
|
||||||
T narrow(U u) noexcept(false)
|
T narrow(U u) noexcept(false)
|
||||||
{
|
{
|
||||||
T t = narrow_cast<T>(u);
|
T t = narrow_cast<T>(u);
|
||||||
|
@ -126,4 +126,8 @@ TEST_CASE("narrow")
|
|||||||
|
|
||||||
n = -42;
|
n = -42;
|
||||||
CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
|
CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
|
||||||
|
|
||||||
|
#if GSL_CONSTEXPR_NARROW
|
||||||
|
static_assert(narrow<char>(120) == 120, "Fix GSL_CONSTEXPR_NARROW");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user