编写饱和的转换运算符而不列出所有可能的组合

时间:2019-06-05 11:04:29

标签: c++ template-specialization

我想在不同类型之间创建模板化操作(假设这是列表:int8_t,int16_t,int32_t,int64_t,uint8_t,uint16_t,uint32_t,uint64_t,float,double)。

然后,我想允许一个saturate_cast<>()函数接受输入值,检查它是否在输出类型限制之内,并在需要时饱和到那些限制。

问题是,如果我将两个int32_t相加,则默认的C ++操作在溢出的情况下具有不确定的行为,因此我想将该操作提升为int64_t并使用该类型执行操作。

一个临时的解决方案可能是:

#include <cstdint>
#include <limits>

template<typename T1, typename T2> struct type_which_fits { using type = decltype(T1() + T2()); };
template<> struct type_which_fits<int32_t, int32_t> { using type = int64_t; };

template<typename T1, typename T2>
auto TAdd(T1 lhs, T2 rhs) {
    using type = typename type_which_fits<T1, T2>::type;
    return static_cast<type>(lhs) + static_cast<type>(rhs);
}

template<typename ODT, typename IDT>
ODT saturate_cast(const IDT& v) {
    if (v > std::numeric_limits<ODT>::max()) {
        return std::numeric_limits<ODT>::max();
    }
    if (v < std::numeric_limits<ODT>::min()) {
        return std::numeric_limits<ODT>::min();
    }

    return static_cast<ODT>(v);
}

int main()
{
    auto x = saturate_cast<int8_t>(TAdd(1, 1u));
    return 0;
}

不幸的是,以这种方式,我需要进一步指定所有可能的类型组合,而我只需要这些规则(以给定的顺序进行验证):

  • 如果其中一种是浮点型,请使用默认促销
  • 如果其中一种类型是64位,请提升为两倍
  • 如果其中一种类型是32位,请提升为64位
  • 否则使用默认促销

此外,当两种类型的“签名”不同时,还会在saturate_cast<>()中弹出一堆签名/未签名警告。

同样可以通过专门研究所有可能的组合来解决此问题,但是在某种程度上感觉是“错误的”。

当我需要更多类型时,您能建议一个解决方案以使其更灵活吗?

1 个答案:

答案 0 :(得分:7)

以下是我的方法:

  1. 查找上级类型(执行操作将产生的类型)

  2. 根据您的规则1-4将类型提升为下一个类型

  3. 在将两面都转换为提升类型时执行添加。


可以通过以下一般规则找到上级类型:

  • 浮点+任何东西->浮点

  • 如果左右位数相同,请选择更大的一位(未签名的节拍签名)

  • 否则选择sizeof()产生最大的位置

最后两个步骤可以通过创建一个辅助结构来保证(并简化),该结构使用std::numerical_limits<T>::digits返回较大的类型(ab),它可以恰好完成我们想要的操作(还涉及带符号/不带符号),因为:

  • std::numerical_limits<int>::digits-> 15

  • std::numerical_limits<unsigned>::digits-> 16

这将相应地适用于所有算术类型。

template<typename T, typename U>
struct larger_arithmetic_type {
    static_assert(std::is_arithmetic_v<T>, "T must be arithmetic");
    static_assert(std::is_arithmetic_v<U>, "U must be arithmetic");
    using type = typename std::conditional_t<(std::numeric_limits<T>::digits < std::numeric_limits<U>::digits), U, T>;
};

template<typename T, typename U>
using larger_arithmetic_type_t = typename larger_arithmetic_type<T, U>::type;

通过此操作,我们可以启用结构arithmetic_superior_type(遵循所述一般规则)以从整数和/或浮点数中查找上级类型:

template<typename T, typename U>
struct arithmetic_superior_type {
    using type = typename 
        std::conditional_t<std::is_floating_point_v<T> && std::is_floating_point_v<U>, larger_arithmetic_type_t<T, U>,
        std::conditional_t<std::is_floating_point_v<T>, T, 
        std::conditional_t<std::is_floating_point_v<U>, U, 
        larger_arithmetic_type_t<T, U>>>>;

};

template<typename T, typename U>
using arithmetic_superior_type_t = typename arithmetic_superior_type<T, U>::type;

因此,arithmetic_seperior_type_t<T, U>返回的类型为+-之间的*/TU

arithmetic_superior_type_t<std::int32_t, float> a;          //-> float
arithmetic_superior_type_t<std::uint32_t, std::int32_t> b;  //-> std::uint32_t
arithmetic_superior_type_t<std::uint32_t, std::uint32_t> c; //-> std::uint32_t
arithmetic_superior_type_t<std::uint64_t, std::uint32_t> d; //-> std::uint64_t
arithmetic_superior_type_t<float, double> e;                //-> double
arithmetic_superior_type_t<std::uint16_t, std::int64_t> f;  //-> std::int64_t

现在,正如您所说,仅此类型是不够的。可能会发生溢出,因此promote_superior_type是第2步,可以从TU接受高级类型的提升-这种类型将明确保留任何加法结果:

template<typename T, typename U>
struct promote_superior_type {
    using superior_type = arithmetic_superior_type_t<T, U>;

    using type = typename
        std::conditional_t<(sizeof(T) == 8u || sizeof(U) == 8u), double, 
        std::conditional_t<std::is_floating_point_v<superior_type>, superior_type, 
        std::conditional_t<(std::numeric_limits<superior_type>::digits < std::numeric_limits<std::int16_t>::digits), std::conditional_t<std::is_signed_v<superior_type>, std::int16_t, std::uint16_t>,
        std::conditional_t<(std::numeric_limits<superior_type>::digits < std::numeric_limits<std::int32_t>::digits), std::conditional_t<std::is_signed_v<superior_type>, std::int32_t, std::uint32_t>,
        std::conditional_t<(std::numeric_limits<superior_type>::digits < std::numeric_limits<std::int64_t>::digits), std::conditional_t<std::is_signed_v<superior_type>, std::int64_t, std::uint64_t>, double>>>>>;
};

template<typename T, typename U>
using promote_superior_type_t = typename promote_superior_type<T, U>::type;

最后,可以添加add<T, U>函数,步骤3:

template<typename T, typename U, typename R = promote_superior_type_t<T, U>>
constexpr R add(T a, U b) {
    return static_cast<R>(a) + static_cast<R>(b);
}

仅此而已。 static_asserting考虑到每种可能的类型匹配,以获得正确的预期输出:

//8_t + U

auto add_i8_i8 =  add(std::int8_t(10),  std::int8_t(10));   //i8 + i8 -> i16
auto add_i8_u8 =  add(std::int8_t(10),  std::uint8_t(10));  //i8 + u8 -> u16
auto add_u8_u8 =  add(std::uint8_t(10), std::uint8_t(10));  //u8 + u8 -> u16
auto add_i8_i16 = add(std::int8_t(10),  std::int16_t(10));  //i8 + i16 -> i32
auto add_i8_u16 = add(std::int8_t(10),  std::uint16_t(10)); //i8 + u16 -> u32
auto add_u8_u16 = add(std::uint8_t(10), std::uint16_t(10)); //u8 + u16 -> u32
auto add_i8_i32 = add(std::int8_t(10),  std::int32_t(10));  //i8 + i32 -> i64
auto add_i8_u32 = add(std::int8_t(10),  std::uint32_t(10)); //i8 + u32 -> u64
auto add_u8_u32 = add(std::uint8_t(10), std::uint32_t(10)); //u8 + u32 -> u64
auto add_i8_i64 = add(std::int8_t(10),  std::int64_t(10));  //i8 + i64 -> d64
auto add_i8_u64 = add(std::int8_t(10),  std::uint64_t(10)); //i8 + u64 -> d64
auto add_u8_u64 = add(std::uint8_t(10), std::uint64_t(10)); //u8 + u64 -> d64
auto add_i8_f32 = add(std::int8_t(10),  float(10));         //i8 + f32 -> f32
auto add_u8_f32 = add(std::uint8_t(10), float(10));         //u8 + f32 -> f32
auto add_i8_d64 = add(std::int8_t(10),  double(10));        //i8 + d64 -> d64
auto add_u8_d64 = add(std::uint8_t(10), double(10));        //u8 + d64 -> d64

//16_t + U

auto add_i16_i16 = add(std::int16_t(10),  std::int16_t(10));  //i16 + i16 -> i32
auto add_i16_u16 = add(std::int16_t(10),  std::uint16_t(10)); //i16 + u16 -> u32
auto add_u16_u16 = add(std::uint16_t(10), std::uint16_t(10)); //u16 + u16 -> u32
auto add_i16_i32 = add(std::int16_t(10),  std::int32_t(10));  //i16 + i32 -> i64
auto add_i16_u32 = add(std::int16_t(10),  std::uint32_t(10)); //i16 + u32 -> u64
auto add_u16_u32 = add(std::uint16_t(10), std::uint32_t(10)); //u16 + u32 -> u64
auto add_i16_i64 = add(std::int16_t(10),  std::int64_t(10));  //i16 + i64 -> d64
auto add_i16_u64 = add(std::int16_t(10),  std::uint64_t(10)); //i16 + u64 -> d64
auto add_u16_u64 = add(std::uint16_t(10), std::uint64_t(10)); //u16 + u64 -> d64
auto add_i16_f32 = add(std::int16_t(10),  float(10));         //i16 + f32 -> f32
auto add_u16_f32 = add(std::uint16_t(10), float(10));         //u16 + f32 -> f32
auto add_i16_d64 = add(std::int16_t(10),  double(10));        //i16 + d64 -> d64
auto add_u16_d64 = add(std::uint16_t(10), double(10));        //u16 + d64 -> d64

//32_t + U

auto add_i32_i32 = add(std::int32_t(10),  std::int32_t(10));  //i32 + i32 -> i64
auto add_i32_u32 = add(std::int32_t(10),  std::uint32_t(10)); //i32 + u32 -> u64
auto add_u32_u32 = add(std::uint32_t(10), std::uint32_t(10)); //u32 + u32 -> u64
auto add_i32_i64 = add(std::int32_t(10),  std::int64_t(10));  //i32 + i64 -> d64
auto add_i32_u64 = add(std::int32_t(10),  std::uint64_t(10)); //i32 + u64 -> d64
auto add_u32_u64 = add(std::uint32_t(10), std::uint64_t(10)); //u32 + u64 -> d64 
auto add_i32_f32 = add(std::int32_t(10),  float(10));         //i32 + f32 -> f32
auto add_u32_f32 = add(std::uint32_t(10), float(10));         //u32 + f32 -> f32
auto add_i32_d64 = add(std::int32_t(10),  double(10));        //i32 + d64 -> d64
auto add_u32_d64 = add(std::uint32_t(10), double(10));        //u32 + d64 -> d64

//64_t + U

auto add_i64_i64 = add(std::int64_t(10),  std::int64_t(10));  //i64 + i64 -> d64
auto add_i64_u64 = add(std::int64_t(10),  std::uint64_t(10)); //i64 + u64 -> d64
auto add_u64_u64 = add(std::uint64_t(10), std::uint64_t(10)); //u64 + u64 -> d64
auto add_i64_f32 = add(std::int64_t(10),  float(10));         //i64 + f32 -> d64
auto add_u64_f32 = add(std::uint64_t(10), float(10));         //u64 + f32 -> d64
auto add_i64_d64 = add(std::int64_t(10),  double(10));        //i64 + d64 -> d64
auto add_u64_d64 = add(std::uint64_t(10), double(10));        //u64 + d64 -> d64


static_assert(std::is_same_v<decltype(add_i8_i8), std::int16_t>, "");
static_assert(std::is_same_v<decltype(add_i8_u8), std::uint16_t>, "");
static_assert(std::is_same_v<decltype(add_u8_u8), std::uint16_t>, "");
static_assert(std::is_same_v<decltype(add_i8_i16), std::int32_t>, "");
static_assert(std::is_same_v<decltype(add_i8_u16), std::uint32_t>, "");
static_assert(std::is_same_v<decltype(add_u8_u16), std::uint32_t>, "");
static_assert(std::is_same_v<decltype(add_i8_i32), std::int64_t>, "");
static_assert(std::is_same_v<decltype(add_i8_u32), std::uint64_t>, "");
static_assert(std::is_same_v<decltype(add_u8_u32), std::uint64_t>, "");
static_assert(std::is_same_v<decltype(add_i8_i64), double>, "");
static_assert(std::is_same_v<decltype(add_i8_u64), double>, "");
static_assert(std::is_same_v<decltype(add_u8_u64), double>, "");
static_assert(std::is_same_v<decltype(add_i8_f32), float>, "");
static_assert(std::is_same_v<decltype(add_u8_f32), float>, "");
static_assert(std::is_same_v<decltype(add_i8_d64), double>, "");
static_assert(std::is_same_v<decltype(add_u8_d64), double>, "");

static_assert(std::is_same_v<decltype(add_i16_i16), std::int32_t>, "");
static_assert(std::is_same_v<decltype(add_i16_u16), std::uint32_t>, "");
static_assert(std::is_same_v<decltype(add_u16_u16), std::uint32_t>, "");
static_assert(std::is_same_v<decltype(add_i16_i32), std::int64_t>, "");
static_assert(std::is_same_v<decltype(add_i16_u32), std::uint64_t>, "");
static_assert(std::is_same_v<decltype(add_u16_u32), std::uint64_t>, "");
static_assert(std::is_same_v<decltype(add_i16_i64), double>, "");
static_assert(std::is_same_v<decltype(add_i16_u64), double>, "");
static_assert(std::is_same_v<decltype(add_u16_u64), double>, "");
static_assert(std::is_same_v<decltype(add_i16_f32), float>, "");
static_assert(std::is_same_v<decltype(add_u16_f32), float>, "");
static_assert(std::is_same_v<decltype(add_i16_d64), double>, "");
static_assert(std::is_same_v<decltype(add_u16_d64), double>, "");

static_assert(std::is_same_v<decltype(add_i32_i32), std::int64_t>, "");
static_assert(std::is_same_v<decltype(add_i32_u32), std::uint64_t>, "");
static_assert(std::is_same_v<decltype(add_u32_u32), std::uint64_t>, "");
static_assert(std::is_same_v<decltype(add_i32_i64), double>, "");
static_assert(std::is_same_v<decltype(add_i32_u64), double>, "");
static_assert(std::is_same_v<decltype(add_u32_u64), double>, "");
static_assert(std::is_same_v<decltype(add_i32_f32), float>, "");
static_assert(std::is_same_v<decltype(add_u32_f32), float>, "");
static_assert(std::is_same_v<decltype(add_i32_d64), double>, "");
static_assert(std::is_same_v<decltype(add_u32_d64), double>, "");

static_assert(std::is_same_v<decltype(add_i64_i64), double>, "");
static_assert(std::is_same_v<decltype(add_i64_u64), double>, "");
static_assert(std::is_same_v<decltype(add_u64_u64), double>, "");
static_assert(std::is_same_v<decltype(add_i64_f32), double>, "");
static_assert(std::is_same_v<decltype(add_u64_f32), double>, "");
static_assert(std::is_same_v<decltype(add_i64_d64), double>, "");
static_assert(std::is_same_v<decltype(add_u64_d64), double>, "");

存在一种不确定性:

i64 + f32 -> d64
u64 + f32 -> d64
//...
static_assert(std::is_same_v<decltype(add_i64_f32), double>, "");
static_assert(std::is_same_v<decltype(add_u64_f32), double>, "");

根据您的规则,也可能是:

i64 + f32 -> f32 
u64 + f32 -> f32 
//...
static_assert(std::is_same_v<decltype(add_i64_f32), float>, "");
static_assert(std::is_same_v<decltype(add_u64_f32), float>, "");

完整代码:

#include <type_traits>
#include <limits>
#include <cstdint>

template<typename T, typename U>
struct larger_arithmetic_type {
    static_assert(std::is_arithmetic_v<T>, "T must be arithmetic");
    static_assert(std::is_arithmetic_v<U>, "U must be arithmetic");
    using type = typename std::conditional_t<(std::numeric_limits<T>::digits < std::numeric_limits<U>::digits), U, T>;
};

template<typename T, typename U>
using larger_arithmetic_type_t = typename larger_arithmetic_type<T, U>::type;

template<typename T, typename U>
struct arithmetic_superior_type {
    using type = typename 
        std::conditional_t<std::is_floating_point_v<T> && std::is_floating_point_v<U>, larger_arithmetic_type_t<T, U>,
        std::conditional_t<std::is_floating_point_v<T>, T, 
        std::conditional_t<std::is_floating_point_v<U>, U, 
        larger_arithmetic_type_t<T, U>>>>;

};

template<typename T, typename U>
using arithmetic_superior_type_t = typename arithmetic_superior_type<T, U>::type;


template<typename T, typename U>
struct promote_superior_type {
    using superior_type = arithmetic_superior_type_t<T, U>;

    using type = typename
        std::conditional_t<(sizeof(T) == 8u || sizeof(U) == 8u), double, 
        std::conditional_t<std::is_floating_point_v<superior_type>, superior_type, 
        std::conditional_t<(std::numeric_limits<superior_type>::digits < std::numeric_limits<std::int16_t>::digits), std::conditional_t<std::is_signed_v<superior_type>, std::int16_t, std::uint16_t>,
        std::conditional_t<(std::numeric_limits<superior_type>::digits < std::numeric_limits<std::int32_t>::digits), std::conditional_t<std::is_signed_v<superior_type>, std::int32_t, std::uint32_t>,
        std::conditional_t<(std::numeric_limits<superior_type>::digits < std::numeric_limits<std::int64_t>::digits), std::conditional_t<std::is_signed_v<superior_type>, std::int64_t, std::uint64_t>, double>>>>>;
};

template<typename T, typename U>
using promote_superior_type_t = typename promote_superior_type<T, U>::type;

template<typename T, typename U, typename R = promote_superior_type_t<T, U>>
constexpr R add(T a, U b) {
    return static_cast<R>(a) + static_cast<R>(b);
}


int main() {
    arithmetic_superior_type_t<std::int32_t, float> a;          //-> float
    arithmetic_superior_type_t<std::uint32_t, std::int32_t> b;  //-> std::uint32_t
    arithmetic_superior_type_t<std::uint32_t, std::uint32_t> c; //-> std::uint32_t
    arithmetic_superior_type_t<std::uint64_t, std::uint32_t> d; //-> std::uint64_t
    arithmetic_superior_type_t<float, double> e;                //-> double
    arithmetic_superior_type_t<std::uint16_t, std::int64_t> f;  //-> std::int64_t


    //8_t + U

    auto add_i8_i8 =  add(std::int8_t(10),  std::int8_t(10));   //i8 + i8 -> i16
    auto add_i8_u8 =  add(std::int8_t(10),  std::uint8_t(10));  //i8 + u8 -> u16
    auto add_u8_u8 =  add(std::uint8_t(10), std::uint8_t(10));  //u8 + u8 -> u16
    auto add_i8_i16 = add(std::int8_t(10),  std::int16_t(10));  //i8 + i16 -> i32
    auto add_i8_u16 = add(std::int8_t(10),  std::uint16_t(10)); //i8 + u16 -> u32
    auto add_u8_u16 = add(std::uint8_t(10), std::uint16_t(10)); //u8 + u16 -> u32
    auto add_i8_i32 = add(std::int8_t(10),  std::int32_t(10));  //i8 + i32 -> i64
    auto add_i8_u32 = add(std::int8_t(10),  std::uint32_t(10)); //i8 + u32 -> u64
    auto add_u8_u32 = add(std::uint8_t(10), std::uint32_t(10)); //u8 + u32 -> u64
    auto add_i8_i64 = add(std::int8_t(10),  std::int64_t(10));  //i8 + i64 -> d64
    auto add_i8_u64 = add(std::int8_t(10),  std::uint64_t(10)); //i8 + u64 -> d64
    auto add_u8_u64 = add(std::uint8_t(10), std::uint64_t(10)); //u8 + u64 -> d64
    auto add_i8_f32 = add(std::int8_t(10),  float(10));         //i8 + f32 -> f32
    auto add_u8_f32 = add(std::uint8_t(10), float(10));         //u8 + f32 -> f32
    auto add_i8_d64 = add(std::int8_t(10),  double(10));        //i8 + d64 -> d64
    auto add_u8_d64 = add(std::uint8_t(10), double(10));        //u8 + d64 -> d64

    //16_t + U

    auto add_i16_i16 = add(std::int16_t(10),  std::int16_t(10));  //i16 + i16 -> i32
    auto add_i16_u16 = add(std::int16_t(10),  std::uint16_t(10)); //i16 + u16 -> u32
    auto add_u16_u16 = add(std::uint16_t(10), std::uint16_t(10)); //u16 + u16 -> u32
    auto add_i16_i32 = add(std::int16_t(10),  std::int32_t(10));  //i16 + i32 -> i64
    auto add_i16_u32 = add(std::int16_t(10),  std::uint32_t(10)); //i16 + u32 -> u64
    auto add_u16_u32 = add(std::uint16_t(10), std::uint32_t(10)); //u16 + u32 -> u64
    auto add_i16_i64 = add(std::int16_t(10),  std::int64_t(10));  //i16 + i64 -> d64
    auto add_i16_u64 = add(std::int16_t(10),  std::uint64_t(10)); //i16 + u64 -> d64
    auto add_u16_u64 = add(std::uint16_t(10), std::uint64_t(10)); //u16 + u64 -> d64
    auto add_i16_f32 = add(std::int16_t(10),  float(10));         //i16 + f32 -> f32
    auto add_u16_f32 = add(std::uint16_t(10), float(10));         //u16 + f32 -> f32
    auto add_i16_d64 = add(std::int16_t(10),  double(10));        //i16 + d64 -> d64
    auto add_u16_d64 = add(std::uint16_t(10), double(10));        //u16 + d64 -> d64

    //32_t + U

    auto add_i32_i32 = add(std::int32_t(10),  std::int32_t(10));  //i32 + i32 -> i64
    auto add_i32_u32 = add(std::int32_t(10),  std::uint32_t(10)); //i32 + u32 -> u64
    auto add_u32_u32 = add(std::uint32_t(10), std::uint32_t(10)); //u32 + u32 -> u64
    auto add_i32_i64 = add(std::int32_t(10),  std::int64_t(10));  //i32 + i64 -> d64
    auto add_i32_u64 = add(std::int32_t(10),  std::uint64_t(10)); //i32 + u64 -> d64
    auto add_u32_u64 = add(std::uint32_t(10), std::uint64_t(10)); //u32 + u64 -> d64 
    auto add_i32_f32 = add(std::int32_t(10),  float(10));         //i32 + f32 -> f32
    auto add_u32_f32 = add(std::uint32_t(10), float(10));         //u32 + f32 -> f32
    auto add_i32_d64 = add(std::int32_t(10),  double(10));        //i32 + d64 -> d64
    auto add_u32_d64 = add(std::uint32_t(10), double(10));        //u32 + d64 -> d64

    //64_t + U

    auto add_i64_i64 = add(std::int64_t(10),  std::int64_t(10));  //i64 + i64 -> d64
    auto add_i64_u64 = add(std::int64_t(10),  std::uint64_t(10)); //i64 + u64 -> d64
    auto add_u64_u64 = add(std::uint64_t(10), std::uint64_t(10)); //u64 + u64 -> d64
    auto add_i64_f32 = add(std::int64_t(10),  float(10));         //i64 + f32 -> d64
    auto add_u64_f32 = add(std::uint64_t(10), float(10));         //u64 + f32 -> d64
    auto add_i64_d64 = add(std::int64_t(10),  double(10));        //i64 + d64 -> d64
    auto add_u64_d64 = add(std::uint64_t(10), double(10));        //u64 + d64 -> d64


    static_assert(std::is_same_v<decltype(add_i8_i8), std::int16_t>, "");
    static_assert(std::is_same_v<decltype(add_i8_u8), std::uint16_t>, "");
    static_assert(std::is_same_v<decltype(add_u8_u8), std::uint16_t>, "");
    static_assert(std::is_same_v<decltype(add_i8_i16), std::int32_t>, "");
    static_assert(std::is_same_v<decltype(add_i8_u16), std::uint32_t>, "");
    static_assert(std::is_same_v<decltype(add_u8_u16), std::uint32_t>, "");
    static_assert(std::is_same_v<decltype(add_i8_i32), std::int64_t>, "");
    static_assert(std::is_same_v<decltype(add_i8_u32), std::uint64_t>, "");
    static_assert(std::is_same_v<decltype(add_u8_u32), std::uint64_t>, "");
    static_assert(std::is_same_v<decltype(add_i8_i64), double>, "");
    static_assert(std::is_same_v<decltype(add_i8_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_u8_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_i8_f32), float>, "");
    static_assert(std::is_same_v<decltype(add_u8_f32), float>, "");
    static_assert(std::is_same_v<decltype(add_i8_d64), double>, "");
    static_assert(std::is_same_v<decltype(add_u8_d64), double>, "");

    static_assert(std::is_same_v<decltype(add_i16_i16), std::int32_t>, "");
    static_assert(std::is_same_v<decltype(add_i16_u16), std::uint32_t>, "");
    static_assert(std::is_same_v<decltype(add_u16_u16), std::uint32_t>, "");
    static_assert(std::is_same_v<decltype(add_i16_i32), std::int64_t>, "");
    static_assert(std::is_same_v<decltype(add_i16_u32), std::uint64_t>, "");
    static_assert(std::is_same_v<decltype(add_u16_u32), std::uint64_t>, "");
    static_assert(std::is_same_v<decltype(add_i16_i64), double>, "");
    static_assert(std::is_same_v<decltype(add_i16_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_u16_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_i16_f32), float>, "");
    static_assert(std::is_same_v<decltype(add_u16_f32), float>, "");
    static_assert(std::is_same_v<decltype(add_i16_d64), double>, "");
    static_assert(std::is_same_v<decltype(add_u16_d64), double>, "");

    static_assert(std::is_same_v<decltype(add_i32_i32), std::int64_t>, "");
    static_assert(std::is_same_v<decltype(add_i32_u32), std::uint64_t>, "");
    static_assert(std::is_same_v<decltype(add_u32_u32), std::uint64_t>, "");
    static_assert(std::is_same_v<decltype(add_i32_i64), double>, "");
    static_assert(std::is_same_v<decltype(add_i32_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_u32_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_i32_f32), float>, "");
    static_assert(std::is_same_v<decltype(add_u32_f32), float>, "");
    static_assert(std::is_same_v<decltype(add_i32_d64), double>, "");
    static_assert(std::is_same_v<decltype(add_u32_d64), double>, "");

    static_assert(std::is_same_v<decltype(add_i64_i64), double>, "");
    static_assert(std::is_same_v<decltype(add_i64_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_u64_u64), double>, "");
    static_assert(std::is_same_v<decltype(add_i64_f32), double>, "");
    static_assert(std::is_same_v<decltype(add_u64_f32), double>, "");
    static_assert(std::is_same_v<decltype(add_i64_d64), double>, "");
    static_assert(std::is_same_v<decltype(add_u64_d64), double>, "");
}