MSVC SDL compliance: Use __pragma(disable:4146) to allow unsigned negation

This commit is contained in:
Ryan Prichard 2016-04-03 21:06:26 -05:00
parent 1bb530f0a1
commit 3d466694e0

View File

@ -65,6 +65,21 @@ struct ValueString {
}
};
#ifdef _MSC_VER
// Disable an MSVC /SDL error that forbids unsigned negation. Signed negation
// invokes undefined behavior for INTxx_MIN, so unsigned negation is simpler to
// reason about. (We assume twos-complement in any case.)
#define STRING_BUILDER_ALLOW_UNSIGNED_NEGATE(x) \
( \
__pragma(warning(push)) \
__pragma(warning(disable:4146)) \
(x) \
__pragma(warning(pop)) \
)
#else
#define STRING_BUILDER_ALLOW_UNSIGNED_NEGATE(x) (x)
#endif
// Formats an integer as decimal without leading zeros.
template <typename C, typename I>
ValueString<C, sizeof(I) * 3 + 1 + 1> gdecOfInt(const I value) {
@ -72,7 +87,7 @@ ValueString<C, sizeof(I) * 3 + 1 + 1> gdecOfInt(const I value) {
auto unsValue = static_cast<U>(value);
const bool isNegative = (value < 0);
if (isNegative) {
unsValue = -unsValue;
unsValue = STRING_BUILDER_ALLOW_UNSIGNED_NEGATE(-unsValue);
}
decltype(gdecOfInt<C, I>(value)) out;
auto &arr = out.m_array;