Skip to content

Instantly share code, notes, and snippets.

View KaixoCode's full-sized avatar

KaixoCode KaixoCode

View GitHub Profile
@KaixoCode
KaixoCode / public_cast.cpp
Created October 20, 2023 19:55
C++ public_cast
/**
* Sneaky public cast, implemented using the auto-return type friend
* function trick to have a stateful compiler. This links the value
* of a private pointer-to-member to a class. This code even works
* at compile-time. Although in Visual Studio, Intellisense seems
* to have trouble some trouble.
*
* Do note that you cannot use this public_cast function for a specific
* member above the `template<> link` specialization. This is because
* we're relying on compiler state, and it reads the file top-down.