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…

`struct Foo f;` and `struct Foo f{};` are very different

Compare the following two programs, and can you predict what the outputs would be for each? // program #1 struct Foo { int x; }; void stack() { int x = 3; } void bar() { struct Foo foo{}; cout << foo.x << endl; } int main() { stack(); bar(); return 0; } // program #2 struct Foo { int x; }; void stack(…