Reminder: When a C++ Object Fails to Construct, the Destructor Does Not Run -- Raymond Chen
In C++, if an object’s constructor fails (due to an exception), destructors are run for the object’s member variables and base classes, but not for the object itself. The principle at play is that you cannot destruct something that was never constructed in the first place.
Reminder: When a C++ Object Fails to Construct, the Destructor Does Not Run
by Raymond Chen
From the article:
Consider this pull request:
com_timeout_t(DWORD timeoutInMilliseconds) : m_threadId(GetCurrentThreadId()) { m_cancelEnablementResult = CoEnableCallCancellation(nullptr); err_policy::HResult(m_cancelEnablementResult); if (SUCCEEDED(m_cancelEnablementResult)) { m_timer.reset(CreateThreadpoolTimer( &com_timeout_t::timer_callback, this, nullptr)); err_policy::LastErrorIfFalse( static_cast<bool>(m_timer)); if (m_timer) { FILETIME ft = filetime::get_system_time(); ft = filetime::add(ft, filetime:: convert_msec_to_100ns(timeoutInMilliseconds)); SetThreadpoolTimer(m_timer.get(), &ft, timeoutInMilliseconds, 0); } } } ~com_timeout_t() { m_timer.reset(); if (SUCCEEDED(m_cancelEnablementResult)) { CoDisableCallCancellation(nullptr); } } ⟦ member variables: ⟧ HRESULT m_cancelEnablementResult{}; DWORD m_threadId{}; bool m_timedOut{}; wil::unique_threadpool_timer_nocancel m_timer;The idea is that the constructor first callsCoEnableCallCancellationand reports the failure viaerr_policy::.HResult()

Bjarne Stroustrup, the creator of C++, has outlined his vision for the language’s future in his article "21st Century C++," emphasizing the need for safer and more modern coding practices without abandoning its powerful legacy. His approach advocates for incremental improvements, such as guideline-enforcing profiles and enhanced type safety, ensuring C++ remains relevant in an era of heightened security and performance demands.
In today's post, I'll learn how modern C++ can influence the code you write for your embedded system. You will see code using up to C++23. The example I show you below circles around at least two questions I got various times from customers: What is consteval good for? What is that user-defined literal operator, and why should I care?



In this blog post, we’ll explore ways to improve the safety of a simple configuration manager. We’ll handle common pitfalls like dangling references and excessive stack usage. Additionally, we’ll see how C++26 helps enforce safer coding practices with stricter diagnostics and improved handling of large objects.