aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/safe_cast.h
blob: 133808ca259346a8dc68a320b36d0322375fb594 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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);
}

}