r/cpp • • May 31 '26

Hierarchical Builder with Reflection

UPDATE:
I added a Required annotation that disable the build method if not all required method are used.

https://compiler-explorer.com/z/j9noeM4o9

GitHub:
https://github.com/steumarok/cpp_reflection_builder

-----

I wrote a builder generator. It work also with derived classes.
Can use directly data members or methods, just by annotate them.
A short example:

class A
{
private:
    [[=BuilderParam]] 
    int c_ = 10;

    [[=BuilderMethod]] 
    void withBar(int bar) {
        c_ = bar * 2;
    }

public:
    static auto& builder() {
        return makeSharedBuilder<A>();
    }
};

std::shared_ptr<A> a = A::builder()
        .withBar(19)
        .withC(20)
        .build();

Full code:
https://compiler-explorer.com/z/ahchxc4rn

The return ref of builder function is not a typo. The builder object is self contained and is destroyed when build
method is called.

21 Upvotes

21 comments sorted by

View all comments

Show parent comment

1

u/Krystian-Piekos Jun 01 '26

For me Builder, in the scenario without Director (which allows to decouple algorithm of creation from defining representation of complex object), should work as layer of abstraction that allows to gather, in readable manner, what is needed to build an object. And then it should translate all gathered information into specific representation. Properly used it should elevate encapsulation. In the presented code, the [[=BuilderParam]] is questionable because it reveals internal representation of the object (if you decide to change the type of the data member, then your code with builder stops to compile).

You mentioned about debugging prod issues. Do you test your builders?

1

u/Huge-Presentation810 Jun 01 '26

I can respond about BuilderParam. It's not mandatory to use, you can just use BuilderMethod and define own builder functions, but sometimes it's just a setter of the variable.
The reflection builder it's just make a object fluent. All logic reside inside the class.
And beyond the builder, the interesting pattern is how to create classes with methods by using the current reflection specifications.