Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

di

> 设计协作
Open source

C++14 Dependency Injection library

1.3K stars0 likes0 views
WebsiteGitHub

About

C++14 Dependency Injection library


boost-ext.di

Your C++14 one header only Dependency Injection library with no dependencies

https://www.youtube.com/watch?v=yVogS4NbL6U


Quick start

Download

boost-ext.di requires only one file. Get the latest header here!

Include

cpp
#include 
namespace di = boost::di;

Compile

  • GCC/Clang
    bash
    $CXX -std=c++14 -O2 -fno-exceptions -fno-rtti -Wall -Werror -pedantic-errors file.cpp
  • MSVC
    bash
    cl /std:c++14 /Ox /W3 file.cpp

Quick guide - Create object graph

cpp
class ctor {
public:
  explicit ctor(int i) : i(i) {}
  int i;
};

struct aggregate {
  double d;
};

class example {
 public:
  example(aggregate a, const ctor& c) {
    assert(87.0 == a.d);
    assert(42 == c.i);
  };
};

int main() {
  const auto injector = di::make_injector(
    di::bind.to(42),
    di::bind.to(87.0)
  );

  injector.create();
}

Run this example on Wandbox.

Clang-3.8
GCC-6
MSVC-2015




Compilation Time
0.102s
0.118s
0.296s




Binary size (stripped)
6.2kb
6.2kb
105kb




ASM x86-64

xor eax, eax retq

Quick guide - Bind interfaces

cpp
struct interface {
  virtual ~interface() noexcept = default;
  virtual int get() const = 0;
};

class implementation : public interface {
public:
  int get() const override { return 42; }
};

struct example {
  example(std::shared_ptr i) {
    assert(42 == i->get());
  }
};

int main() {
  const auto injector = di::make_injector(
    di::bind.to()
  );

  injector.create>();
}

Run this example on Wandbox.

Clang-3.8
GCC-6
MSVC-2015




Compilation Time
0.102s
0.118s
0.296s




Binary size (stripped)
6.2kb
6.2kb
105kb




ASM x86-64 (same as `make_unique`)

push %rbx mov %rdi,%rbx mov $0x8,%edi callq 0x4008e0 movq $0x400c78,(%rax) mov %rax,(%rbx) mov %rbx,%rax pop %rbx retq

Quick guide - Bind templates

…

Run this example on Wandbox.

Clang-3.8
GCC-6
MSVC-2015




Compilation Time
0.102s
0.118s
0.296s




Binary size (stripped)
6.2kb
6.2kb
105kb




ASM x86-64

xor eax, eax retq

Quick guide - Bind concepts

cpp
struct Streamable {
 template
 auto requires(T&& t) -> decltype(
   int( t.read() ),
   t.write(int)
 );
};

template
class example {
public:
  example(Exchange exchange, Engine engine)
    : exchange(std::move(exchange)), engine(std::move(engine))
  { }

private:
  Exchange exchange;
  Engine engine;
};

int main() {
  const auto injector = di::make_injector(
    di::bind.to(),
    di::bind.to()
  );

  injector.create();
}

Run this example on Wandbox.

Clang-3.8
GCC-6
MSVC-2015




Compilation Time
0.102s
0.118s
0.296s




Binary size (stripped)
6.2kb
6.2kb
105kb




ASM x86-64

xor eax, eax retq


Documentation

  • Introduction
    • Do I use a Dependency Injection already?
    • Do I use Dependency Injection correctly?
    • Do I need a Dependency Injection?
    • STUPID vs SOLID - "Clean Code" Uncle Bob
    • Do I need a DI Framework/Library?
    • Manual DI - Wiring Mess (Avoid it by using [Boost].DI)
    • Real Life examples?
    • Why [Boost].DI?
    • [Boost].DI design goals
    • Articles
    • Videos
    • [Boost::ext].DI
    • Dependency Injection In General
    • Acknowledgements
  • Overview
    • Quick Start
    • Dependencies
    • Supported/Tested compilers
    • Configuration
    • Exception Safety
    • Thread Safety
    • Error Messages
    • Performance
  • Tutorial
    • 0. [Pre] Refactor towards DI
    • 1. [Basic] Create objects tree
    • 2. [Basic] First steps with bindings
    • 3. [Basic] Decide the life times
    • 4. [Basic] Annotations to the rescue
    • 5. [Basic] Split your configuration
    • 6. [Advanced] Dump/Limit your types
    • 7. [Advanced] Customize it
    • 8. [Advanced] Extend it
  • Try It Online!
  • Benchmarks
    • Performance
    • C++ Libraries
    • C++ vs Java vs CLibraries
    • Usage of C++ vs Java vs CLibraries
  • User Guide
    • Injector
    • Bindings
    • Injections
    • Annotations
    • Scopes
    • Modules
    • Providers
    • Policies
    • Concepts
    • Configuration
  • Examples
    • Hello World
    • Bindings
    • Dynamic Bindings
    • Forward Bindings
    • Is Creatable
    • Multiple Bindings
    • Binding Non-owning Pointer
    • Binding Templates
    • Binding To Constructor
    • Automatic Injection
    • Constructor Signature
    • Constructor Injection
    • Multiple Interface
    • Annotations
    • Deduce Scope
    • Custom Scope
    • Eager Singletons
    • Modules
    • Modules (hpp/cpp)
    • Custom Policy
    • Custom Provider
    • Pool Provider
    • Configuration
    • Polymorphism
    • Inheritance
    • Type Erasure
    • Function
    • Variant
    • Templates
    • Concepts
  • Extensions
    • Injector
    • Constructor Bindings
    • Contextual Bindings
    • Assisted Injection
    • Extensible Injector
    • Concepts
    • Factory
    • Shared Factory
    • Lazy
    • Named Parameters
    • XML Injection
    • Serialize
    • Types Dumper
    • UML Dumper
    • [Heap Prov

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++dependency-injectiondesign-patterns

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category设计协作
PricingOpen source

> Related tools

F
Figma
协作式界面设计工具
S
sketch-wakatime
Sketch plugin for automatic time tracking and metrics generated from your Sketch usage.
Q
QuickColor
Quickly apply fills from the global or document color palettes to selected elements - 🎨