• Advertisement
Sign in to follow this  

Which C++ cast to use for T * to T *__restrict?

This topic is 1475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For example, I have a char * and I want to cast it to a char *__restrict.

Visual C++ 2012 seems to be OK with either static_cast or const_cast, and doesn't require a reinterpret_cast, but which one is really correct to use?

Share this post


Link to post
Share on other sites
Advertisement

__restrict is a non-standard extension specific for MSVC. So, anything that works will do; when casting act as if the __restrict wouldn't be there, unless the compiler complains (i.e. don't use reinterpret_cast when you could use static_cast, and try to avoid const_cast and C casts).

 

If you're thinking into porting to other compilers, look at other compiler's requirements (i.e. on gcc, it is __restrict__)

Edited by Matias Goldberg

Share this post


Link to post
Share on other sites

On the other hand, casting from a restrict pointer to a non-restrict pointer is something you most probably don't want to do ever, so again, no cast needed.
 
Restrict pointers are a promise that your pointers do not alias anything else in the same scope, so the compiler can optimize more aggressively and leave out loads/stores that it would otherwise have to do in order to ensure correctness. Casting away restrict means first making a promise and then finding a "legal" way to break the promise anyway. In my opinion, that is just asking for trouble. Never lie to the compiler and never try to trick it. I'd rather not use anything like restrict in the first place in such a case.

Casting away restrict does not violate the semantics of restrict. It doesn't make the pointer alias with something. What is does is it removes information passed to the compiler. It may be necessary to implicitly remove __restrict when calling a function with that pointer that does not have a restrict version. And when a __restrict version exists, casting from restrict would de-optimize the code, which may be useful for debugging. In principle, it may also deoptimize any non-call code using the cast pointer within the function, but function analysis is likely to see though such a cast, and still treat the pointer as __restrict.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement