Šimon Tóth’s Post

View profile for Šimon Tóth

C++ Educational Content Creator | 20 years of Software Engineering experience distilled into digestible daily posts

Compile-time constant expressions are not permitted to invoke undefined behaviour. This includes constexpr functions that are evaluated at compile-time. This property can be used to statically test code, ensuring that the code doesn't invoke undefined behaviour. Compiler Explorer link: https://lnkd.in/ecjTtfkG #cpp #cplusplus #coding #programming #dailybiteofcpp

  • text

Does this mean that if there is a possibility that the function will trigger a UB, it is safer to mark it as `constexpr` to have some compile-time circuit breaker?

Removing constexpr keywords from the example lets the code compile. Which is peculiar. Why can't the compiler check for UB when compiling non-constexpr code? The value of a can be calculated at compile time, so can b and c's. It is obvious that calling midpoint with a and b would cause UB. Why must the compiler require constexpr context? Is this not possible, or is it; but not implemented yet?

All over this of late, great stuff. "constexpr all the things"

See more comments

To view or add a comment, sign in

Explore content categories