Timeline for answer to When should I use reinterpret_cast? by jalf
Current License: CC BY-SA 4.0
Post Revisions
19 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 11, 2021 at 7:59 | comment | added | Andrew Truckle |
So can I use static_cast here: const auto* pItem = (NM_GRIDVIEW*)pNotifyStruct;?
|
|
| Dec 13, 2020 at 18:29 | comment | added | Max Barraclough | static_casting a pointer to and from void* preserves the address. In the interests of precision: some pointer types cannot safely and portably be cast to and from void*, no matter which kind of cast is used. This is the case for any pointer-to-function type or pointer-to-member-function type. isocpp.org/wiki/faq/… | |
| Sep 15, 2019 at 16:50 | comment | added | Avi Ginsburg |
For an example where the value of b differs, see (shameless plug) my answer below.
|
|
| S Aug 30, 2019 at 8:57 | history | suggested | leiyc | CC BY-SA 4.0 |
improved formatting
|
| Aug 30, 2019 at 7:34 | review | Suggested edits | |||
| S Aug 30, 2019 at 8:57 | |||||
| Jun 12, 2017 at 10:18 | comment | added | Binary Mind |
static_cast refuses to cast function pointers to void* and back. reinterpret_cast and C-style casting (void*) work for this case.
|
|
| Jan 30, 2017 at 21:07 | comment | added | Loki Astari |
@HelinWang Which is why using of reinterpret_cast is a big red flag in your code you know its dangerous and it reminds you to check. static_cast does not throw as big a and red a flag and thus is more dangerous because you are not prompted to remember that what you have is really horrendous.
|
|
| Jan 30, 2017 at 15:54 | comment | added | Helin Wang | @LokiAstari I think unspecified does not stop you from doing silly things. It only stop you when you remember it's unspecified. Huge difference. Personally I don't like unspecified. Too much to remember. | |
| Apr 15, 2016 at 17:56 | comment | added | einpoklum | This does not actually answer the question of "when to use reinterpret_cast". | |
| May 11, 2013 at 17:20 | comment | added | David | What happens if you mix the two cast? Is that undefined behavior? stackoverflow.com/questions/16499683/… | |
| Nov 22, 2011 at 17:46 | history | edited | user142019 | CC BY-SA 3.0 |
aaaaaarrrrrrgrgghhh
|
| Oct 29, 2011 at 9:29 | comment | added | Stack Overflow is garbage |
hmm, true about reinterpret-casting to/from void*. I wonder why I thought that was legal when I wrote this.
|
|
| Oct 28, 2011 at 23:20 | comment | added | Johannes Schaub - litb |
the value of b is not unspecified anymore in C++11 when using reinterpret_cast. And in C++03 a cast of int* to void* was forbidden to be done with reinterpret_cast (although compilers did not implement that and it was impractical, hence was changed for C++11).
|
|
| Apr 30, 2009 at 3:35 | comment | added | Michael Burr | @Martin - reinterpret_cast<> is not guaranteed to result in the same bit pattern. "The mapping performed by reinterpret_cast<> is implementation defined." (C++03 5.3.10). However, the standard notes that "it is intended to be unsurprising". | |
| Feb 21, 2009 at 21:37 | comment | added | Loki Astari | I thought that reinterpret_cast<> guaranteed the same bit pattern. (which is not the same as a valid pointer to another type). | |
| Feb 21, 2009 at 21:27 | comment | added | Loki Astari | I like the fact that 'b' is undefined. It stops you doing silly things with it. If you cast something to another pointer type you are asking for problems and the fact that you can not depend on it makes you more careful. If you had used static_cast<> above what use is the 'b' anyway? | |
| Feb 21, 2009 at 19:08 | history | edited | Stack Overflow is garbage | CC BY-SA 2.5 |
added 33 characters in body
|
| Feb 21, 2009 at 18:41 | vote | accept | HeretoLearn | ||
| Feb 21, 2009 at 16:42 | history | answered | Stack Overflow is garbage | CC BY-SA 2.5 |