The following code fails to compile in both MSVC2015 and clang when the comment marks are removed but it compiles as it is.
int main()
{
static_assert( alignof( int * ) == alignof( int * * ), "nope" );
const int * * a = nullptr;
//const int * * * b = reinterpret_cast< const int * * * >( a );
auto c = static_cast< const int * * * >( static_cast< void * >( a ) );
return 0;
}
This question is different from a previously asked one because there is not overall const
qualifier being casted away.
According to the standard [expr.reinterpret.cast]/7
An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue
v
of object pointer type is converted to the object pointer type “pointer tocv T
”, the result isstatic_cast<cv T*>(static_cast<cv void*>(v))
.
In this case, the target “pointer to cv T
” is const int * * *
which makes T = const int * *
and no cv
qualifiers. Thus, the result should be static_cast<T*>(static_cast<void*>(v))
.
There are constrains about the alignment of T
, but those are not relevant here as demonstrated in the static assert. Since the result of reinterpret_cast< const int * * * >( a )
can actually be computed using the intermediate steps, the commented code should compile if uncommented.
Where is the error in my reasoning (if any)?