Tuesday, April 7, 2026

Reusing code in const and non-const methods, C++17

I found the code below from this great answer in StackOverflow. It allows you to implement the const method with your logic, then use this one-liner to implement the non-const method. It’s a trick from the great Scott Meyers:

const T& f() const {
	return something_complicated();
}

T& f() {
	return const_cast<T&>(std::as_const(*this).f());
}

I’m using this thing everywhere.

Thursday, April 2, 2026

Project templates in Visual Studio 2022

After configuring C and C++ projects countless times, base on these settings, I finally bit the bullet and created a reusable template in Visual Studio 2022. And it’s surprisingly simple: create the project and then go Project → Export Template.

The project name itself can be used inside files with $safeprojectname$ string. In order to have it replaced even inside .rc files, edit the .vstemplate and add:

<TemplateContent>
  <Project TargetFileName="ayylmao.vcxproj" File="ayylmao.vcxproj" ReplaceParameters="true">
    <ProjectItem ReplaceParameters="true">RES\$safeprojectname$.rc</ProjectItem>
  </Project>
</TemplateContent>

There’s no GUI for deleting a template, though. One must manually remove the zip files from two different locations:

  • Documents \ My Exported Templates
  • Documents \ Templates \ Project Templates

To restore a template, simply copy the zip back to:

  • Documents \ Templates \ Project Templates