/
githubmirror
/
omim
Обзор
Документация
Войти
/
githubmirror
/
omim
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
android-95
base/checked_cast.hpp
32 строки
1 KB
Добрый Ээх
[base] fix signed/unsigned checked_cast
25 янв 2017, 13:43
25 янв 2017, 13:43
afca613
Код
Авторство
О чём код?
#pragma once #include "base/assert.hpp" #include <type_traits> namespace base { template <typename ReturnType, typename ParameterType> ReturnType checked_cast(ParameterType 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) { 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