TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25333 20
Hierarchical Builder with Reflection

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 makeBuilder<A>();
}
};

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

Full code:
[https://compiler-explorer.com/z/Pq94EKq9h](https://compiler-explorer.com/z/Pq94EKq9h)


https://redd.it/1tt5o76
@r_cpp
Compiler-Explorer Compiler Explorer - C++ (x86-64 gcc 16.1) // // A builder with C++26 reflection. // Written by stefano.marocco@gmail.com // template<typename B, typename T> struct BuildMethod; template<typename B, typename T, auto Fn> struct WithMethod; struct BuilderParamT {}; struct BuilderMethodT {}; static…
More from @r_cpp
  1. Sep 29, 2026Boost.Graph 1.95 will be C++17 Dear Boost.Graph community, In two release cycles (Boost re…
  2. Sep 26, 2026Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C…
  3. Sep 25, 2026myStringStream.str("") Considered Harmful Under C++20 I was recently looking at some (rath…
  4. Sep 20, 2026A clever branch free optimization I'm the developer of memlz which is an extremely fast co…
  5. Sep 15, 2026Inside Boost.PolyCollection https://bannalia.blogspot.com/2026/09/inside-boostpolycollecti…
  6. Sep 11, 2026C++26: Standard Library Hardening Experiments https://www.cppstories.com/2026/hardening-ex…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →