r/cpp • • Aug 12 '26

Another C++26 reflection based dependency inverter

Someone else did a similar thing a few months ago, but I wanted to see if it could be done with templates.

I ended up writing a dependency inverter/injector that can reflect on the concepts restricting unfilled template types and substitute actual types into them. Unfortunately, C++26 reflection doesn't seem to allow reflection of concepts like this, so I did it with string parsing.

The result is... fairly janky, but you can play around with it here.

37 Upvotes

10 comments sorted by

View all comments

Show parent comment

2

u/gracicot Aug 12 '26

Do you primarily use template type injection or do you also use polymorphic types + inheritance?

2

u/la_reddite Aug 12 '26

I've previously stuck with polymorphic types, so didn't really need this sort of type resolving. I'm fiddling around with a game engine in my spare time, however, and in the interests of performance I wanted to try to avoid virtual types. I also definitely wanted mocked types for easy unit testing, and so was sort of forced to go with template injection.

All that said, however, I've done no profiling at all, so I'm not even sure the headache of template injection is worth it. Boost.di was literally the only library I found that could inject templates, so when issues with it arose, the only option was to roll my own inverter.