3 ms·
What do those languages return when you calculate MAX_INT + 1?
by marcosdumay 11y ago
What do those languages return when you calculate MAX_INT + 1?
- steveklabnik 11y agoIn Rust: let x = std::u32::MAX + 1; This line will generate a warning upon build, and in a debug build, will panic. In a release build, it will overflow.
- drewcrawford 11y agoIn Swift: let a = Int.max + 1 Produces a compile error as the overflow is within Swift's conception of a "literal expression". If we step out to a case where the overflow is not trivially a literal expression: func foo(a: Int) -> Int { return Int.max + a } foo(1) This causes a runtime assertion failure in optimized or unoptimized builds. There is a (rarely-used) "Ounchecked" optimization level for which this program produces UB.