Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorДобрый Ээх <bukharaev@gmail.com>2017-01-24 12:48:39 +0300
committerSergey Yershov <syershov@maps.me>2017-01-25 13:43:05 +0300
commitafca613c57be5df8768b675df7015788ca64fe2e (patch)
treeb8872c135a5aa43c28a24c23ab647c6cf10d8d4d /base
parent7b2725e2f277f6fa9d86f609740717d106c38ea6 (diff)
[base] fix signed/unsigned checked_cast
Diffstat (limited to 'base')
-rw-r--r--base/checked_cast.hpp20
1 files changed, 16 insertions, 4 deletions
diff --git a/base/checked_cast.hpp b/base/checked_cast.hpp
index 363840c785..bc37f13418 100644
--- a/base/checked_cast.hpp
+++ b/base/checked_cast.hpp
@@ -2,19 +2,31 @@
#include "base/assert.hpp"
+#include <type_traits>
+
namespace base
{
template <typename ReturnType, typename ParameterType>
ReturnType checked_cast(ParameterType v)
{
- CHECK_EQUAL(static_cast<ParameterType>(static_cast<ReturnType>(v)), v, ());
- return static_cast<ReturnType>(v);
+ static_assert(std::is_integral<ParameterType>::value, "ParameterType should be integral");
+ static_assert(std::is_integral<ReturnType>::value, "ReturnType should be integral");
+
+ ReturnType const result = static_cast<ReturnType>(v);
+ CHECK_EQUAL(static_cast<ParameterType>(result), v, ());
+ CHECK((result > 0) == (v > 0), ("checked_cast failed, value =", v, ", result =", result));
+ return result;
}
template <typename ReturnType, typename ParameterType>
ReturnType asserted_cast(ParameterType v)
{
- ASSERT_EQUAL(static_cast<ParameterType>(static_cast<ReturnType>(v)), v, ());
- return static_cast<ReturnType>(v);
+ static_assert(std::is_integral<ParameterType>::value, "ParameterType should be integral");
+ static_assert(std::is_integral<ReturnType>::value, "ReturnType should be integral");
+
+ ReturnType const result = static_cast<ReturnType>(v);
+ ASSERT_EQUAL(static_cast<ParameterType>(result), v, ());
+ ASSERT((result > 0) == (v > 0), ("asserted_cast failed, value =", v, ", result =", result));
+ return result;
}
} // namespace base