C++

C++26 Adds a Standard Header With Three Functions

C++26 introduces a <debugging> header that standardizes how programs trigger breakpoints and detect an attached debugger, replacing the custom macros many projects maintain today.

Until now, hitting a breakpoint from code has required platform-specific tricks, and major projects have each built their own wrapper. Boost.Test, Catch2, Dear ImGui and Unreal all ship their own macros or functions for it. C++26 addresses this through proposals P2546R5 and P2810R4, credited to Rene Ferdinand Rivera Morell.

The new header provides three functions, all noexcept and freestanding, but none constexpr because debugging happens at runtime. std::breakpoint() halts execution unconditionally when a debugger is attached; on x86 it usually becomes a single int3 instruction. Without a debugger, behavior is implementation-defined, and on many platforms the process receives a signal such as SIGTRAP and ends unless the signal is handled.

std::is_debugger_present() reports whether a debugger is currently tracing the program. It checks on every call, since a debugger can attach or detach at any moment. The standard does not prescribe the mechanism. The function is also replaceable, like operator new and operator delete, so bare-metal platforms without an OS-level way to detect a debugger can supply their own version.

std::breakpoint_if_debugging() combines the two: it breaks only if a debugger is present. Because is_debugger_present() is separate, it can also be used to add diagnostic output, disable timeouts, switch allocators or skip optimizations while debugging. The author sees the header as a small but overdue addition.

Source: Hacker News (front page)


Posted

in

by

Tags: