简体   繁体   中英

How to distinguish unsigned int from uint32_t

I need to find all code which is using as size other than uint32_t . I've made my custom SizeStub type with uint32_t inside. And started code replace. To avoid false alerts on code where I already have uint32_t I've added next operators:

template <typename T>
SizeStub& operator += (const T& in)
{
    static_assert(std::is_same<T, decltype(_value)>::value, "NOT SAME");
    this->_value += in;
    return *this;
}

Also binary operators, etc.

Everything is great. I have compile time alerts for all types except uin32_t and ... unsigned int . So. How to distinguish unsigned int which is not fixed size?

gcc version 4.8.2

PS The plan is to replace all SizeStub usage when I'll fix all compile time alerts onto uint32_t .

uint32_t is (afaik always) a typedef to the unsigned 32 bit integer type available on the platform you compile for.

Do in your case uint32_t is defined via typedef unsigned int uint32_t;

So uint32_t is the same type as unsigned int , just using another name.

EDIT: Accidently submitted before I was done...

You might have some possibilities on how to achieve your goal:

  1. You could switch to a platform where uint32_t isn't the same type as unsigned int . This might work, yet you need to be careful. For example if you are using MSVC as compiler unsigned int is always a 32 bit unsigned integer type - regardless of compiling for 32 or 64 bit.
  2. You could try to redefine uint32_t to a distinct type that can handle (assign, cast, ...) values that are of type unsigned int . This would make you aware of uneqal cases... Yet it might complex to find the correct position in code to redefine uint32_t so your compiler will fail on all cases you want it to fail.
  3. In addition to 2: You could temporarly(!) alter the original typedef. Maybe you want to set up a seperate build environment to do this, so your "real" build environment isn't affected by it.

Maybe someone can come up with a better solution.

If unsigned int is 32 bits, then it and uint32_t are the same type . You can't differentiate between one type.

Since uint32_t is only a type alias, this is comparable to typedef xy where you can't differentiate between x and y .

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