大佬教程收集整理的这篇文章主要介绍了c – 确定临时模板的大小,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
template<typename T> T interpolate(uint32_t distance,const T& from,const T& to);
这里,距离从0到0xFFFF.
现在,我想为常见类型的T提供理智的实现,例如uint32_t.所以我编写了一个示例实现(忽略它仅针对签名类型实现):
template<typename T> inline auto interpolate(uint32_t distance,const T& to) -> std::enable_if_t<std::is_signed<T>::value,T> { return from + ((distance * (to - from)) / 0xFFFF); }
这适用于小于32位的类型,因为距离永远不会大于16位,而距离*(来自 – )的结果是uint32_t.但是32位不足以包含0xFFFF * uint32_t的结果 – 在这种情况下,模板需要64位临时值.
有没有一种标准化的方法我可以选择比“T更大”的类型来保存临时结果?显然,这只需要适用于内在类型;用户的类型仍然需要自己的插值实现.
template<typename T> T interpolate(uint16_t factor,const T& to);
请注意,在进行插值时,结果可能会超出[from;因此,界面中不需要更大的类型.
现在,你通过整数插值使你的生活变得复杂.我不知道为什么你不想要浮点插值(特别是对于动画框架,我们不再是1995年了!).
所以,如果你真的想要整数风格,那么你需要一个更大的整数来保持它.你能做的最简单:
template<typename I> struct bigger_Integer; template<> struct bigger_Integer<int8_t> { typedef int16_t type; }; template<> struct bigger_Integer<int16_t> { typedef int32_t type; }; template<> struct bigger_Integer<int32_t> { typedef int64_t type; }; template<> struct bigger_Integer<uint8_t> { typedef uint16_t type; }; template<> struct bigger_Integer<uint16_t> { typedef uint32_t type; }; template<> struct bigger_Integer<uint32_t> { typedef uint64_t type; };
并且,在您的函数中,使用以下更大的类型:
using I = std::make_signed_t<typename bigger_Integer<T>::type>;
它甚至适用于经过微小修改的无符号类型.
我的意见:使用双打.如果在分析会话后发现它们导致性能问题,那么您可以尝试优化它们.但老实说我怀疑情况会是这样.这里的代码更简单:
template<typename T> T interpolate(uint16_t factor,const T& to) { return T(double(from) + double(factor) * (double(to) - double(from)) / double(0xFFFF)); }
以上是大佬教程为你收集整理的c – 确定临时模板的大小全部内容,希望文章能够帮你解决c – 确定临时模板的大小所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。