我想计算转换因子。为此,我必须将最大值除以按uchar的最大值ushort。
我想通过将参数传递给函数或typename来动态地执行此操作。然后我想选择最大值并执行计算。
有两个问题:
已知所有值都适合双倍范围。
理想情况下,我想做的事情如下:
double x = numeric_limits<T>::max / numeric_limits<T2>::max;
然而,这是不正确/可能的。
答案 0 :(得分:2)
你提出的想法应该有效:
#include <iostream>
#include <limits>
template <typename T, typename T2>
double get_ratio()
{
return static_cast<double>(std::numeric_limits<T>::max()) / std::numeric_limits<T2>::max();
}
int main()
{
auto ratio = get_ratio<unsigned short, unsigned char>();
std::cout << ratio << '\n';
return 0;
}