Access a function's return value in C++ scope guard

It is best to avoid accessing a function's return value in a scope guard or we need to be really careful about the return value's lifetime. Happy case Scope guard is a simple RAII concept in C++ that, in most cases, just works. struct ScopeGuard { ScopeGuard(std::vector& v): v_

When `__cxa_pure_virtual` is just a different flavor of SEGFAULT

A program crashed during shutdown, with message pure virtual method called which came from __cxa_pure_virtual – where a pure virtual function points to, in its vtable. Its implementation involves calling std::terminate which calls std::abort which by default throws a SIGABRT, and crashing the program. Now, why did…

C++ coroutine and lambda's lifetime

We rarely need to worry about lambda's lifetime more than any other objects in C++ until we are dealing with coroutine at the same time. folly's wiki has a good example of why we need to be careful about lambda's lifetime – https://github.com/facebook/folly/blob/main/folly/experimental/…

What I learned about `inline` recently

C++'s inline is pretty nice, in my opinion. I define an inline function with external linkage, things work as expected (https://en.cppreference.com/w/cpp/language/inline): * taking the address of the inline function works * function-local static objects work as expected (shared across all translation units) * no ODR…

`defaulted` constructor in C++

You have seen Foo() = default;. It declares a default constructor (duh!). But what does it really do, and when is it actually useful? "When is it useful" is a very interesting question in my opinion because next time when you see = default; in other people's code, you would understand why…

C++ exception (3) – catching an exception

This is the third post of a series that I am making on C++ exceptions. C++ exception (1) — zero-cost exception handlingThis is the first post of a series I am making on C++ exceptions. C++ exception (1) — zero-cost exception handlingThis is the first post of a series I am making…

C++ exception (2) — throwing an exception

This is the second post of a series that I am making on C++ exceptions. C++ exception (1) — zero-cost exception handlingThis is the first post of a series I am making on C++ exceptions. C++ exception (1) — zero-cost exception handlingThis is the first post of a series I am making…

C++ exception (1) — zero-cost exception handling

This is the first post of a series I am making on C++ exceptions. C++ exception (1) — zero-cost exception handlingThis is the first post of a series I am making on C++ exceptions. C++ exception (2) — throwing an exceptionThis is the second post of a series that I am making…