@@ -566,4 +566,56 @@ void test_constructor_field_initializers() {
566566 ConstructorTest l6; // Test third constructor
567567 ConstructorTest l7 (0 ); // Test fourth constructor
568568 ConstructorTest l8 (l1, l1, l3); // Test fifth constructor
569+ }
570+
571+ // Test explicit casts
572+ void test_explicit_casts () {
573+ std::uint8_t l1 = 42 ;
574+ std::uint16_t l2 = 1000 ;
575+ std::int8_t l3 = -10 ;
576+ std::int32_t l4 = -100 ;
577+ float l5 = 3 .14f ;
578+ double l6 = 2.718 ;
579+
580+ // Explicit cast expressions are treated as expressions, not id-expressions
581+ u8 = static_cast <std::uint8_t >(l2); // COMPLIANT
582+ u16 = static_cast <std::uint16_t >(l1); // COMPLIANT
583+ s8 = static_cast <std::int8_t >(l4); // COMPLIANT
584+ s32 = static_cast <std::int32_t >(l3); // COMPLIANT
585+ s32 = static_cast <std::int16_t >(l3); // NON_COMPLIANT
586+
587+ // Type category conversions with explicit casts
588+ f = static_cast <float >(l4); // COMPLIANT
589+ s32 = static_cast <std::int32_t >(l5); // COMPLIANT
590+
591+ // Size conversions with explicit casts
592+ d = static_cast <double >(l5); // COMPLIANT
593+ l5 = static_cast <float >(l6); // COMPLIANT
594+
595+ // C-style casts (also expressions)
596+ u8 = (std::uint8_t )l2; // COMPLIANT
597+ s8 = (std::int8_t )l4; // COMPLIANT
598+ f = (float )l4; // COMPLIANT
599+
600+ // Functional style casts (also expressions)
601+ u8 = std::uint8_t (l2); // COMPLIANT
602+ s8 = std::int8_t (l4); // COMPLIANT
603+ f = float (l4); // COMPLIANT
604+
605+ // Const_cast (creates expressions)
606+ const std::uint8_t l7 = 100 ;
607+ u8 = const_cast <std::uint8_t &>(l7); // COMPLIANT
608+
609+ // Reinterpret_cast (creates expressions)
610+ u32 = reinterpret_cast <std::uint32_t &>(l4); // COMPLIANT
611+
612+ // Assignment to variables through explicit casts
613+ std::uint32_t l8;
614+ std::uint16_t l9;
615+ l8 = static_cast <std::uint32_t >(l9); // COMPLIANT
616+ l9 = static_cast <std::uint16_t >(l8); // COMPLIANT
617+
618+ // Function calls with explicit casts
619+ f1 (static_cast <std::int64_t >(l4)); // COMPLIANT
620+ f2 (static_cast <std::int32_t >(l4)); // COMPLIANT
569621}
0 commit comments