C ++如何阻止负数作为参数传递,其中unsigned是预期的 [英] C++ How to block negative numbers being passed as argument where unsigned is expected

查看:175
本文介绍了C ++如何阻止负数作为参数传递,其中unsigned是预期的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这个线程,但是情况是完全不同的,我不能应用这个解决方案我的问题。

i already saw this thread, however the situation is totally different and i cant apply that solution to my problem.

我有以下构造函数:

Fan::Fan(Id id, std::string name, Age age);

其中Id和Age是typedef的unsigned int和unsigned short。
这是给我,所以我知道我必须使用它们,因为很可能测试者的赋值将尝试使用大于int(在unsigned int / short范围内)。

where Id and Age are typedef'ed unsigned int and unsigned short. this is given to me, so i know that i must use them, as most likely the tester of the assignment will try to use numbers bigger than int (in range of unsigned int / short).

显然,粉丝的ID和年龄不能是负数。
在示例代码中(编译时),创建一个带有id和age的数字的风扇。我不能使用字符串,并检查一个减号。

obviously, a Fan's id and age cannot be negative numbers. in the example code (to compile against), a fan is created with numbers for id and age. i cannot use string there and check for a minus sign.

我认为它不会编译时,我输入负年龄/ id在我的单元测试。然而,它原来是它编译,它给了随机值(很可能是由于溢出原因)。

i thought it wouldnt compile when i entered negative age / id in my unit testing. however, it turned out it did compile, and it gave random values (most likely due to overflow reasons).

所以总结 - 有可能调用构造函数年龄和ID的负值,但在构造函数中,它们的值被th ashed,随机数字出现,并导致意外的行为。

so to conclude - it is possible to call the constructor with negative values for the age and id, but in the constructor their values get 'thrashed' and random numbers appear, and it causes unintended behaviour.

/ p>

for example, this:

Fan* fan = new Fan(-1, "Name", 9);

编译,并且在runetime风扇的id获得一些不相关的值。所以我必须能够检测到构造函数中的负数。
我如何阻止构造函数中的负数?

compiles, and at runetime the id of the fan gets some unrelated value. so i must be able to detect the negative number in the constructor. how do i 'block' the negative numbers in the constructor?

感谢您的时间!我希望我的问题很清楚

Thanks for your time! i hope my question is clear

推荐答案

我创建了一个小函数模板,检查输入是否在输出数据的范围内类型,否则抛出异常。它要求输入和输出数据类型是整数,并且输入数据类型的大小大于输出数据类型。

I created a small function template that checks whether the input lies within the range of the output data type, and throws an exception otherwise. It requires that the input and output data types are integral, and that the size of the input data type is larger than the output data type.

template<typename DestType, typename SrcType>
DestType range_check_and_convert(SrcType const& src)
{
    static_assert(sizeof(SrcType) > sizeof(DestType), 
        "SrcType must be larger than DestType");
    static_assert(std::is_integral<SrcType>::value &&
                  std::is_integral<DestType>::value, "integral types only");

    if(src > static_cast<SrcType>(std::numeric_limits<DestType>::max())) {
        throw std::out_of_range("input too big");
    } else if(src < static_cast<SrcType>(std::numeric_limits<DestType>::min())) {
        throw std::out_of_range("input too small");
    }

    return static_cast<DestType>(src);
}

演示

这篇关于C ++如何阻止负数作为参数传递,其中unsigned是预期的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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