允许以下行为:
int * const p1 = nullptr;
auto p2 = static_cast< const int * const >( ptr );
在我的理解中
p1
是一个“const-ptr to int”,它被转换为
“const-ptr 到 const-int”.
为什么以下内容被禁止
int * * const pp1 = nullptr;
auto pp2 = static_cast< const int * * const >( ptr );
在我的理解中
pp1
是一个“const-ptr to ptr to int”,它将被转换为“const-ptr to ptr to const-int”。因此我添加了const
,所以我认为这是允许的。
因为那是伪装的
const_cast
。观察:
int **const pp1 = nullptr;
auto pp2 = (const int **const)pp1;
const int x = 42;
*pp2 = &x;
**pp1 = 43; // Modifying `x` = undefined behavior.
请注意,另一方面,允许强制转换为
const int *const *
(和 const int *const *const
)。