我能否在 uint_fast64_t 和无符号长整型之间实现类型等效性
Can I achieve type equivalency between uint_fast64_t and unsigned long long?
我有一个大型代码库,其中包含使用 uint_fast64_t 作为索引类型的矩阵相关代码。要使用 GMM 作为求解器后端,我需要从以下位置转换(投射!)向量:
std::vector<uint_fast64_t, std::allocator<uint_fast64_t>>
到 GMM 内部格式
std::vector<unsigned long long, std::allocator<unsigned long long>>
在 MSVC2012 下,uint_fast64_t 被 typedef'd 为无符号长长,因此两个表达式"相同"。
我很清楚它们实际上不是同一类型,因为无符号长可能正好是 64 位长(长)(因为它是实现定义的),而uint_fast64_t至少是 64 位长。( --不仇恨者 ;) )
可悲的是,GCC4.7和Clang 3.4 uint_fast64_t为内部类型,使得任何类型的铸造都是不可能的。
此外,似乎在某些时候,clang 将uint_fast64_t解释为无符号长 - 使其与无符号长长定义更加不兼容。
你从我的痛苦中看到了哪些方法?
我唯一的选择是手动替换GMM代码中的无符号长长吗?
下面是一个不太可移植但有效的解决方案:
static_assert(sizeof(uint_fast64_t) == sizeof(unsigned long long), "This code relies on equivalence of uint_fast64_t and unsigned long long");
std::vector<uint_fast64_t, std::allocator<uint_fast64_t>> src;
std::vector<unsigned long long, std::allocator<unsigned long long>> &after_cast =
*reinterpret_cast<std::vector<unsigned long long, std::allocator<unsigned long long>> *>(&src);
您必须在关闭严格类型别名的情况下编译它(例如 Clang 的 -fno-strict-aliasing
)。
它依赖于标准中未定义的行为,但通过传递适当的编译器标志,您实际上可以使编译器提供一致的定义。
static_cast
应该可以解决问题,因为它们都是数字类型(前提是您的 fast64 整数使用不超过 64 位)。
unsigned long long convert_to_ull(uint_fast64_t i)
{
return static_cast<unsigned long long>(i);
}
std::vector<uint_fast64_t> origData;
// fill in origData
std::vector<unsigned long long> data;
data.reserve(origData.size());
std::transform(origData.begin(), origData.end(), data.begin(), convert_to_ull);
相关文章:
- C 字符串返回字符串的整数/双精度/长整型值
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 将长整型值打印为带有前导零的十六进制
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 提升不良词法强制转换:将字符串转换为无符号长整型时,无法将源类型值解释为目标
- 将最小值整数转换为无符号长整型
- 如何将小端格式的QByteArray转换为无符号长整型
- 无符号长整型和无符号 int 之间有什么区别,这 2 种类型应该如何在 c# 中封送?
- 环礁和(长整型)的区别?
- 将逗号格式化为长整型整数
- 如何将长整型传递给 itoa 函数?
- 是否存在将长整型转换为指针有效的情况
- 在 C++ 中创建一对长整型和矢量时出现编译错误
- 当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好
- 将字符串中以十六进制形式存储的负长整型值转换为 C++ 中的长整型变量
- 为什么当我们把变量"u"的数据类型从长整型更改为整型时,模幂会出错?
- 错误:请求从 std::chrono::time_point 浮点数转换为非标量类型长整型
- 如何以优雅高效的方式将无符号/有符号整数/长整型转换为 C 字符串?
- 我能否在 uint_fast64_t 和无符号长整型之间实现类型等效性