ClickHouse/src/Common/safe_cast.h
Azat Khuzhin 4e76629aaf Fixes for -Wshorten-64-to-32
- lots of static_cast
- add safe_cast
- types adjustments
  - config
  - IStorage::read/watch
  - ...
- some TODO's (to convert types in future)

P.S. That was quite a journey...

v2: fixes after rebase
v3: fix conflicts after #42308 merged
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
2022-10-21 13:25:19 +02:00

23 lines
382 B
C++

#pragma once
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
template <class To, class From>
To safe_cast(From from)
{
constexpr auto max = std::numeric_limits<To>::max();
if (from > max)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Overflow ({} > {})", from, max);
return static_cast<To>(from);
}
}