mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-15 10:52:30 +00:00
23 lines
382 B
C++
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);
|
||
|
}
|
||
|
|
||
|
}
|