Summary
This episode explains how to safely treat non-const objects as const when you need to call const-qualified member functions. Jason addresses feedback from viewers who claimed they needed const_cast to add const-qualification, and demonstrates why std::as_const (added in C++17) is a much safer alternative. The episode shows how to implement as_const yourself for pre-C++17 codebases, discusses issues with using const_cast or static_cast for this purpose, and explains the legitimate use cases for explicitly calling const member functions on non-const objects.
Related C++ Standard Sections
This episode covers topics found in these sections of the C++ standard:
-
[utility.as.const]75% match