如何将大于 std::numeric_limits<fundamental_type>::max() 的值传递给函数?
How to pass values greater than std::numeric_limits<fundamental_type>::max() to a function?
我正在编写一个函数void func(K const& val)
,它是模板类的一部分。
假设我将类对象定义为foo<unsigned short int> f;
,然后将函数调用为f.func(999999);
发生的情况是值999999
被转换为其他值,因为它本身超出了范围。如何防止这种情况发生?代码中是否有编译器标志或其他方式可以防止这种情况发生?
检查
if(val > std::numeric_limits<K>::max())
func
内部的值没有帮助,因为在传递该值时,该值已经转换为数值限制内的值。
例如,99999 898989899999转换为11823
如何将大于std::numeric_limits::max()的值传递给函数?
附言:我正在使用gcc(带有-std=c++11)编译
假设类foo
的定义类似于:
template<typename K>
class foo {
public:
void func(K const& k) {}
};
您可以自己制作func
模板,并在调用实际实现之前检查限制:
#include <iostream>
#include <limits>
template<typename K>
class foo {
public:
template<typename T>
std::enable_if_t<std::is_signed<T>::value && !std::is_signed<K>::value> func(T const& t)
{
if (t < 0 || static_cast<std::make_unsigned_t<T>>(t) > std::numeric_limits<K>::max()) {
std::cout << "errorn";
return;
}
func_impl(t);
}
template<typename T>
std::enable_if_t<!std::is_signed<T>::value && std::is_signed<K>::value> func(T const& t)
{
if (t > static_cast<std::make_unsigned_t<K>>(std::numeric_limits<K>::max())) {
std::cout << "errorn";
return;
}
func_impl(t);
}
template<typename T>
std::enable_if_t<std::is_signed<T>::value == std::is_signed<K>::value> func(T const& k)
{
if (k < std::numeric_limits<K>::min() || k > std::numeric_limits<K>::max()) {
std::cout << "errorn";
return;
}
func_impl(k);
}
private:
void func_impl(K const& k)
{
std::cout << "normaln";
}
};
int main()
{
foo<char>().func(127);
foo<char>().func(127u);
foo<char>().func(-128);
foo<char>().func(128);
foo<char>().func(128u);
foo<char>().func(-129);
foo<unsigned>().func(-1);
foo<int>().func(1u);
}
输出:
正常
正常
正常
错误
错误
错误
错误
正常
实时
编辑
正如@BaumMitAugen所指出的,在我的func
实现中,boost::numeric_cast可能是手动if
的更好替代方案——如果发生下溢或上溢,它将引发异常,并且您将避免许多最新编辑版本的代码样板,这些代码(a)有效,(b)避免有符号/无符号的比较警告。
相关文章:
- 为什么可以将左值传递给"std::async",即使它引用了右值
- 当值传递给C++中的运算符重载函数时会发生什么
- 如何将非可变参数值传递给 fmt::format?
- 将 qml 项目值传递给 cpp 后如何与它们进行交互?
- NDK:将文本字段值传递给 c++ 字符串
- 如何将值传递给 lambda 函数
- 如何在C++中将std::string值传递给RegSetValueEx()
- 如何将 c ++ 变量的值传递给 hadoop HDFS 的 bash 系统命令?
- C++ 通过参数将值传递给 char 数组
- 如何将值传递给MFC消息映射函数on_command
- 使用JNI将长值传递给Java
- 如何在不使用 Argv 和 Argc 的情况下将 Sum 值传递给 C++ 程序
- C++ 将值传递给 fStream
- 如何将函数中的多个值传递给C++中的不同函数
- 将多个解析值传递给 addPositionalArgument 函数
- C++为什么将左值传递给move构造函数对模板有效
- 一种自动检测对象的方法,该方法通过值传递给c++中的函数
- 将值传递给main(int,char**)
- 将大于long类型的最大值的值传递给C中的fseek
- 将值传递给atexit