Luca
Luca

Reputation: 10996

casting const pointers between different types

I have a C++ function where one of the input parameters is of type char const* buffer. The way I understand it is that the underlying values of this array can be changed but the pointer itself cannot be moved to point to something else.

Now what I want to do is reinterpret this array as unsigned short and do some manipulations. So, I do something like:

char const * current = &buffer[currentLine]; // Points to some location
unsigned short * const values = static_cast<unsigned short * const>(current);
// Change some of these values

This results in invalid cast from top char * const to type short unsigned int * const.

How should this casting be performed?

Upvotes: 2

Views: 4475

Answers (2)

Fran&#231;ois Andrieux
Fran&#231;ois Andrieux

Reputation: 29023

To help you with type safety, each cast has a limited set of conversions that it will allow. This allows you to help communicate what you intend to do with the cast which lets the compiler warn you if you would accidentally preform a conversion that wasn't intended.

static_cast is a relatively safe casting operator. It allows the most common and safest conversions, including those that are already implicitly allowed. This does not include converting between unrelated pointers types, which is often dangerous. The correct cast operator to use is reinterpret_cast. The reinterpret_cast is used when you want to interpret some memory as a given type, regardless of how type of object that memory actually represents. Among other things, it's used when you convert between unrelated pointer types.

Remember that, when given the choice, it's always preferred to use the most restrictive cast operator that will preform the work you need done. This will give the compiler the most opportunities to notify you if you make a mistake.

Note that you are converting between pointer types. It's important not to confuse converting between two types and converting between two pointers to those types. The way to convert between char and unsigned short is not the same way you convert between pointer to char and pointer to unsigned short.

Upvotes: 2

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

The way I understand it is that the underlying values of this array can be changed but the pointer itself cannot be moved to point to something else.

Nope. It means that you cannot change the entity this pointer points too, but you are able to change a pointer itself. To forbid changing the pointer you have to make the pointer itself const:

const char* const buffer;
            ^^^^^

How should this casting be performed?

The casting should be performed with reinterpret_cast.

Only the following conversions can be done with reinterpret_cast, except when such conversions would cast away constness or volatility.

...

5) Any pointer to object of type T1 can be converted to pointer to object of another type cv T2.

So you have to write:

unsigned short * const values = reinterpret_cast<unsigned short * const>(current);

or even:

decltype(auto) values = reinterpret_cast<unsigned short * const>(current);

Finally, static_cast is not applicable here because you try to perform a cast between different unrelated pointer types.

Upvotes: 4

Related Questions