简体   繁体   中英

Converting Between Incompatible Types

In some places in my code, I use the _mm_prefetch function, which takes in a const char * and int . The first parameter being the memory address of the object to prefetch. Since it's a const char * instead of void* , it needs to be cast.

Note: in the below context nextActive is a pointer to object.

Originally, I had:

_mm_prefetch( (const char *)this->nextActive, _MM_HINT_T1 );

This works fine; however, this leads to a warning, regarding C++ Core Guidelines Pro.safety: Type-safety profile Type.4.

I could use reinterpret_cast , but, that results in a warning violating Type 1a of the above ( Don't use reinterpret_cast )

Then, I thought, well, I can cast to void* then to const char* , but that results in a warning of Type 1d ( Don't cast between pointer types when the conversion could be implicit )

_mm_prefetch( static_cast<const char *>( static_cast<void *>( this->nextActive ) ), _MM_HINT_T1 );

Short of just suppressing the warnings when using _mm_prefetch , is there a proper way to covert between two incompatible types? Or are there always going to be warnings, because (of course) you probably shouldn't be casting between different types?

This should fix it:

void* ptr = this->nextActive;
_mm_prefetch(static_cast<const char*>(ptr), _MM_HINT_T1);

The only cast is from void* to const char* which is allowed.

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