简体   繁体   中英

c++ Need for typecasting int to unsigned int

I had this code as part of a C++ project

unsigned int fn() {
    //do some computations
    int value = ....

    if(value <= 0)
        return 0;
    return (unsigned int)value;
}

I don't get the need to use the cast at the last return statement since all negative numbers will be caught in the if statement(hence returning 0).

And moreover this function fn is called from another function (whose return type is int ) that simply returns the value returned by fn .

Thanks..

目的是使编译器警告静音,否则可能会发出警告。

我认为它真的没有改变,我亲自为不同的场景运行相同的代码,看起来最后的演员阵容可以完成。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM