DEV Community

Anek Green
Anek Green

Posted on

"C with classes" revisited

Ever wondered why these Cold War era C/C++ stacks are so messy? They consist of trivial preprocessing, unstable linking, perverse templates, non-determined-exactly-size-but-less-than-you-need integers and more.
When writing https://git.sr.ht/~anek/werewolf (the Boost.Beast/Lua bridge), I've unavoidingly stumbled at every embedded C++ coder' issue: writing C++ wrappers of C libraries. Let's look at my code:

void lua_vm::getglobal(const std::string &key)
{
    lua_getglobal(state(), key.c_str());
}

void lua_vm::push_string(const std::string &str) {
    lua_pushlstring(state(), str.data(), str.size());
}

void lua_vm::push_number(size_t num) {
    lua_pushnumber(state(), num);
}
Enter fullscreen mode Exit fullscreen mode

Looks familiar? Hell yeah.

What if we revisit C++ namespaces/classes/structs/preprocessing?

namespace context {
        struct context *create();
        void delete(struct context *);
        int method1(struct context *ctx, int arg1);
        int method2(struct context *ctx, int arg2);
}
struct context {
        int field1;
        int field2;
};
class context {
        using namespace context;
        using struct context;
};
Enter fullscreen mode Exit fullscreen mode

Working name for this spontaneous attempt (I'm late at night, you know) is Holy C++.

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay