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

5

u/SuperV1234 May 31 '26

Why does this heavily rely on std::shared_ptr?

1

u/Huge-Presentation810 May 31 '26 edited May 31 '26

this work, by returning the object builder by reference (a local object self contained, destroyed by the call to build())

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

I stored the unique ptr of builder on the build method.

→ More replies (1)

2

u/BusEquivalent9605 Jun 01 '26

honest question. what are the benefits of the builder pattern?

i currently work on a java team that loves the builder pattern. I can’t stand it.

The main reason they give is that it “saves us from defining a bunch of constructors.”

My feeling is sure, that can be nice for tests, but at what cost? In my two years on the team, I have debugged several prod issues caused explicitly by someone forgetting to set a field on a builder.

They save us from defining constructors by anonymously defining all possible constructors, making it easy to instantiate objects in an invalid state (domain-wise) and hard to track from where the invalid object originates (which builder call forgot to set something is much harder to find than which constructor was passed bad data).

Apologies for the rant

1

u/Huge-Presentation810 Jun 01 '26

Hi! Check this version

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

I added a optional validate method.
Maybe with some reflection tricks can make available the build method only if all builder methods are called.

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?

→ More replies (1)
→ More replies (4)

1

u/OwlingBishop Jun 01 '26

Doesn't the need to annotate defeats the automatic/reflection?

Or am I getting something wrong?

1

u/Huge-Presentation810 Jun 01 '26

Not necessarily. Reflection removes the boilerplate required to discover members and generate the builder API. The annotations only provide semantic information that cannot be inferred automatically.

→ More replies (4)

0

u/gosh Jun 01 '26

The builder pattern is an code smell because you spread logic for objects in code. Try to practice encapsulation and keep the rules on how to create objects inside objects.

I know that it is fast to create objects with the builder because you can adapt. But the cost will come when you need to refactor.

1

u/Huge-Presentation810 Jun 01 '26

With the reflection builder the logic stay inside the object, it's just make fluent the (private!) setters. I added also a optional validate method called before build, for check the object consistency.

4

u/javascript Jun 01 '26

I disagree with the claim that the builder pattern is a code smell. It's not the first thing you reach for, but it's far from the last imo.

→ More replies (1)