From Jason Turner
Watch on YouTube

Summary

This episode explores how to apply the [[nodiscard]] attribute to constructors in C++20, which warns when constructor results are ignored. Jason demonstrates how this differs from applying [[nodiscard]] to an entire type and shows examples using unique_lock and unique_ptr. This feature is particularly useful for constructors that acquire resources, ensuring that users don't accidentally discard objects that have acquired a resource, preventing potential resource leaks.

Resources

Tags