use static_cast<pointer type>(void *) not reinterpret_cast

Description

Umesh & Andy have convinced me that we should use a static_cast to go from `void *` to a specific pointer type, rather than a reinterpret_cast. I have been using reinterpret_cast for this. We should change such uses of reinterpret_cast to static_cast.

Umesh & Andy provided the following quotes. A `void *` is seen as a "related type" to another, more specific pointer type.

Stroustrup (section 7.7) gives this example:

Also, in section 6.2.7, he gives

From Stroustrup's book section 6.2.7, "static_cast converts between related types such as one pointer type to another in the same class hierarchy, an integral type to an enumeration, or a floating point type to an integral type".

From "The Annotated C++ Reference Manual" by Stroustrup and Margaret A. Ellis: (It is a little outdated: 1995)

Section 5.2.4:

... `static_cast<T>(e)` casts `e` to a `T` exactly as `(T) e` would have done provided that there is an implicit conversion from `T` to the type of `e` or an implicit conversion from the type of `e` to `T`. Otherwise, `static_cast<T>(e)` is an error.

Activity

Show:
TracBot
June 30, 2018, 11:47 PM
Trac Comment 1 by —2012-07-25T18:15:21.185Z

Should add a note to the coding guidelines.

TracBot
June 30, 2018, 11:47 PM
Trac Comment 3 by —2012-08-08T00:59:55.666Z

Changes made across ICU4C.

I added some text in the coding guidelines, section Pointer Conversions: http://userguide.icu-project.org/dev/codingguidelines#TOC-C-and-C-Type-and-Format-Convention-Guidelines

Fixed

Assignee

Markus Scherer

Reporter

Markus Scherer

Components

Labels

None

Reviewer

None

Priority

medium

Time Needed

Hours

Fix versions