In C++, How Can I Make a Default Parameter be the This Pointer of the Caller? -- Raymond Chen
In C++, associating member objects like properties or events with their containing class often requires passing this redundantly. This article explores a generalized, flexible solution using templates, variadic arguments, and deducing this to streamline ownership initialization without boilerplate.
In C++, How Can I Make a Default Parameter be the This Pointer of the Caller? Revisited
by Raymond Chen
From the article:
Some time ago, we looked at making the default parameter of a method be the
thispointer of the caller. The scenario was something like this:struct Property { Property(char const* name, int initial, Object* owner) : m_name(name), m_value(initial), m_owner(owner) {} ⟦ other methods elided - use your imagination ⟧ char const* m_name; Object* m_owner; int m_value; }; struct Widget : Object { Property Height{ "Height", 10, this }; Property Width{ "Width", 10, this }; };and we didn’t want to have to type
thisas the last parameter to all thePropertyconstructors. We came up with this:template<typename D> struct PropertyHelper { Property Prop(char const* name, int initial) { return Property(name, initial, static_cast<D*>(this)); } }; struct Widget : Object, PropertyHelper<Widget> { Property Height = Prop("Height", 10); Property Width = Prop("Width", 10); };


Jonathan Müller attended the fall 2024 meeting of the ISO C++ standardization committee in Wrocław, Poland. This was the fifth meeting for the upcoming C++26 standard and the feature freeze for major C++26 features.
While most time zones use simple hour offsets from UTC, some regions have chosen unusual time differences. In this blog post, we’ll explore how we can discover such zones using C++20’s chrono library.
In this blog post, we will explore handling dates using std::chrono, including time zones. We’ll utilize the latest features of the library to retrieve the current time across various time zones, taking into account daylight saving time changes as well. Additionally, we will incorporate new capabilities introduced in C++23, such as enhanced printing functions and more.