ReviseAlgo Logo

Modern C++ Features

C++23 Features

Discover features introduced in C++23, including std::print, explicit object parameters, and std::expected.

Interview: Monadic error handling with std::expected, explicit object parameters (deducing this), and std::print formatting optimizations.

Last Updated: June 13, 2026 9 min read

The C++23 standard modernizes the language, introducing monadic error handling via std::expected, faster output streams via std::print, and explicit object parameters.

std::print

A fast formatting utility in <print> that replaces slow C++ iostreams like std::cout.

std::expected

A return type in <expected> that represents either a valid value or an error code, supporting functional error handling.

Deducing this

Allows member functions to accept the current object (this) as an explicit parameter, simplifying CRTP designs.

Monadic Error Handling: std::expected

Traditionally, APIs handle errors by returning codes or throwing exceptions. std::expected<T, E> provides a modern alternative: it holds a valid value of type T or an error details object of type E.

This allows you to write clean error-handling logic without throwing exceptions or checking magic return codes.

Code Walkthrough

Demonstrates using std::expected for error handling and explicit object parameters in C++23.

#include <iostream>
#include <expected>
#include <string>
#include <print> // C++23: print headers

enum class ParseError { InvalidChar, EmptyInput };

// C++23 expected: returns integer value or ParseError code std::expected<int, ParseError> parseNumber(const std::string& str) { if (str.empty()) return std::unexpected(ParseError::EmptyInput);

for (char c : str) { if (c < '0' || c > '9') return std::unexpected(ParseError::InvalidChar); } return std::stoi(str); }

class Widget { public: // C++23 Deducing this: explicit object parameter template <typename Self> void log(this Self&& self) { std::println("Widget processed."); // Prints formatted string with newline } };

int main() { auto res = parseNumber("12a4"); if (!res) { if (res.error() == ParseError::InvalidChar) { std::println(stderr, "Error: Input contains invalid characters."); } } else { std::println("Parsed value: {}", *res); }

Widget w; w.log(); // Implicitly passes object pointer via deducing this

return 0; }

Interview-Relevant Information

Q: How does std::print improve upon std::cout and printf?
Answer: std::print is type-safe and supports modern formatting syntax (like Python's str.format). It compiles to smaller binary footprints than std::cout and bypasses the overhead of local formatting locales, making it faster than both printf and stream insertion.

Q: What problem does "deducing this" solve?
Answer: Previously, to support const and non-const method overloads, you had to duplicate the function implementation. "Deducing this" allows you to write a single templated function that deduces the object's constness and ref-qualifiers automatically, simplifying your code.

Quick Checklist

Do you use std::print for console output? Have you replaced legacy error checks with std::expected? If yes, your code is optimized for C++23.

Use Cases

Designing robust APIs that return error codes or values without throwing exceptions.

Optimizing logging systems in high-frequency trading platforms using std::print.

Common Mistakes

Throwing generic exceptions for recoverable logic errors instead of returning std::expected.

Duplicating member functions to support const and non-const overloads instead of using deducing this.