Josef “Jeff” Sipek

Useless reinterpret_cast in C++

A few months ago (for whatever reason, I didn’t publish this post earlier), I happened to stumble on some C++ code that I had to modify. While trying to make things work, I happened to get code that essentially was:

uintptr_t x = ...;
uintptr_t y = reinterpret_cast<uintptr_t>(x);

Yes, the cast is useless. The actual code I had was much more complicated and it wasn’t immediately obvious that ‘x’ was already a uintptr_t. Thinking about it now, I would expect GCC to give a warning about a useless cast. What I did not expect was what I got:

foo.cpp:189:3: error: invalid cast from type "uintptr_t {aka long unsigned int}"
    to type "uintptr_t {aka long unsigned int}"

Huh? To me it seems a bit silly that the compiler does not know how to convert from one type to the same type. (For what it’s worth, this is GCC 4.6.2.)

Can anyone who knows more about GCC and/or C++ shed some light on this?


  1. Your code makes no sense: `reinterpret_cast` is for casts between pointer types, but `uintptr_t` is just an integral type capable of holding a pointer. Use `static_cast` if `x` isn't a pointer, but some number.

    Comment by Benjamin — January 26, 2013 @ 12:36

  2. Hrm. I see... I still think that the error message could be clearer.

    Comment by JeffPC — January 27, 2013 @ 02:22

Atom feed for comments on this post.

Leave a comment

Powered by blahgd