Menu

C++ under the Hood: Zero-Cost Abstractions vs. Real Cost
📰

C++ under the Hood: Zero-Cost Abstractions vs. Real Cost

DEV Community: cpp·Uwe·14 days ago
#7LcrVWhU
#devtocpp#devto#dev#hood#cost
Reading 0:00
15s threshold

Uwe

Posted on Nov 1

Edited on Nov 1

Bjarne Stroustrup famous design axiom for C++ still holds true: "You don’t pay for what you don’t use. And what you do use, you couldn’t hand-code any better."

However, modern C++ features vary wildly. Some are completely eliminated by the compiler (Zero-Cost), some introduce a negligible wrapper cost, and others carry heavy runtime penalties if used carelessly.

Here is a first incomplete classification from a hardware and assembly perspective, compared directly to pure C:

1.Zero-Cost Abstractions (0% Runtime Overhead vs. C)

These language features exist purely at compile time. In the final machine code, they are indistinguishable from hand-written C code.

References (& vs. Pointers):

Under the hood: Resolved entirely by the optimizer into raw memory addresses ( mov / lea ). A reference consumes zero memory and zero extra clock cycles at runtime.

C++20 Concepts & Templates:

Under the hood: Compile-time polymorphism.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.