☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

通过 glvalue 访问对象,但 glvalue 的类型不符合要求

C++-Undefined-Behavior-23
目录 › next › previous

只应通过以下类型的 glvalue 访问对象,否则导致未定义的行为:

  • 对象的动态类型
  • 用 const 或 volatile 限定的对象动态类型
  • 与对象动态类型相似的类型(参见 ISO/IEC 14882:2011 4.4)
  • 与对象动态类型对应的有符号或无符号类型
  • 用 const 或 volatile 限定的与对象动态类型对应的有符号或无符号类型
  • 包含上述类型的聚合或联合类型(不包括静态成员类型)
  • 对象动态类型的基类类型(也包括被 const 或 volatile 限定的基类类型)
  • char 或 unsigned char

示例:

int i = 0;

cout << i;                 // Well-defined
cout << (const int&)i;     // Well-defined
cout << (unsigned int&)i;  // Well-defined
cout << (char&)i;          // Well-defined

cout << (long&)i;          // Undefined behavior
cout << (float&&)i;        // Undefined behavior

依据

ISO/IEC 14882:2003 3.10(15)-undefined ISO/IEC 14882:2011 3.10(10)-undefined

规则

ID_castNoInheritance
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.