TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24826 15
illustrates the anti-pattern:

struct NetworkSystem
{
NetworkSystem(Gx::Context& ioc) // DON'T DO THIS. Stick with the example I provided above
{
config = ioc.Require<Config>();
logger = ioc.Require<Logger>();
timer = ioc.Require<Timer>();
profiler = ioc.Require<Profiler>();
}

Config* config; Logger* logger; Timer* timer; Profiler *profiler;
};

auto ioc = Gx::Context();
auto networkSystem = NetworkSystem(ioc); // just don't

The above case is an anti-pattern because **it hides dependencies.** When a class receives the entire container, its constructor signature no longer tells you what it actually needs, which defeats the purpose of [DI](https://en.wikipedia.org/wiki/Dependency_injection). IoC container should be primarily used in the root composition of your classes' initialization (e.g, your `main()`).

In addition, many IoC containers perform compile-time checks to some extent regardless of the language. By passing the container directly, you are giving up compile-time checks that the library can otherwise perform (e.g., `ioc.Require<NetworkSystem>()` may fail at compile-time if one of the dependencies is not constructible either by the library (multiple ambiguous constructors) or by the nature of the class itself). I think we all could agree that we should enforce compile-time checks whenever possible.

Just like other programming patterns, some exceptions may apply, and it might be more practical to go with anti-pattern in some particular situations (that's why `Require<T>` in my lib is exposed anyway, it could be used for different purposes).

There might be other anti-patterns I couldn't remember off the top of my head, but the above is the most common mistake. There are a bunch of resources online that discuss this.

This is a pretty common concept for web dev folk (and maybe gamedev?), but I guess it is not for your typical C++ dev

https://redd.it/1ro288e
@r_cpp
Wikipedia Dependency injection technique in software engineering
More from @r_cpp
  1. Oct 7, 2026Full Meeting C++ 2026 program released https://meetingcpp.com/mcpp/schedule/#2026 https://…
  2. Oct 7, 2026Qt moc now can handle basic C++ modules (Qt 6.12) https://codereview.qt-project.org/c/qt/q…
  3. Oct 7, 2026New to c++ Hello everybody,im new to cpp,and want to learn it,i have some knowledge with C…
  4. Oct 7, 2026Optimizing the Compressed Log Pipeline: https://github.com/Tencent/BqLog/blob/main/docs/Ar…
  5. Oct 7, 2026BqLog 2.6.0: a C++ logging library running on 30M+ devices daily, benchmarked against quil…
  6. Oct 7, 2026BqLog 2.6.0: a C++ logging library running on 30M+ devices daily, benchmarked against quil…
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 →