Profile Mikhail Zaytsev
GitHub

Public cast or how to break C++ wonderfully awful ways

One day I learned that access specifiers in C++ are not checked during template instantiation, and it immediately seemed like a way to break incapsulation. So I set out to create public_cast

Setting the stage

To be more formal with what I'm aiming to achieve, I'm going to provide an ideal API that I would like to see when the project is finished. After some consideration, I decided on something like this:

class C {
private:
    int x = 57;
    void f() { std::cout << "secret\n"; }
};

ENABLE_PUBLIC_CAST(C, x);
ENABLE_PUBLIC_CAST(C, f);

int main() {
    auto res = GET_MEMBER_POINTER(C, x);
    auto func = GET_MEMBER_POINTER(C, f);
    C c;
    std::cout << c.*res << '\n';
    (c.*func)();

    return 0;
}