C ++整数类型是给定类型宽度的两倍 [英] C++ integer type twice the width of a given type

查看:51
本文介绍了C ++整数类型是给定类型宽度的两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中, coord_squared_t 是整数类型的别名,其大小至少是整数类型 coord_t 的两倍:

In this example, coord_squared_t is the alias for an integer type with at least twice the size of the integer type coord_t:

typedef int_least32_t coord_t;

coord_squared_t CalculateSquaredHypothenuse(coord_t x, coord_t y){
    coord_squared_t _x=x;
    coord_squared_t _y=y;
    return _x*_x+_y*_y;
}

用什么来表达 coord_squared_t ?在标准库中有什么可以让我做类似 double_width< coord_t> :: type 的事情来获取正确的宽度,而不是显式地选择类型吗?

What could be used to express coord_squared_t in terms of coord_t? Is there anything in the standard library that allows me to do something like double_width<coord_t>::type to get the correct width, instead of explicitly choosing the type?

C ++ 11或C ++ 14很好.

C++11 or C++14 are fine.

推荐答案

您可以使用

You could use boost::int_t:

using coord_squared_t = boost::int_t<sizeof(coord_t)*CHAR_BIT*2>::least;

这篇关于C ++整数类型是给定类型宽度的两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆