C++14 依赖注入库
--------------------------------------- boost-ext.di 仅需一个头文件的 C++14 依赖注入库,无任何依赖 https://www.youtube.com/watch?v=yVogS4NbL6U --- 快速入门 下载 boost-ext.di 只需要一个文件。获取最新的头文件! 编译 GCC/Clang sh $CXX -std=c++14 -O2 -fno-exceptions -fno-rtti -Wall -Werror -pedantic-errors file.cpp MSVC sh cl /std:c++14 /Ox /W3 file.cpp 快速指南 - 创建对象图 cpp 类 ctor { public: explicit ctor(int i) : i(i) {} int i; }; 结构体 aggregate { double d; }; 类 example { public: example(aggregate a, const ctor& c) { assert(87.0 == a.d); assert(42 == c.i); }; }; int main() { const auto injector = di::makeinjector( di::bind.to(42), di::bind.to(87.0) ); injector.create(); } 在 Wandbox 上运行此示例。 Clang-3.8 GCC-6 MSVC-2015 编译时间 0.102s 0.118s 0.296s 二进制大小(去除) 6.2kb 6.2kb 105kb ASM x86-64 xor eax, eax retq 快速指南 - 绑定接口 cpp 结构 interface { virtual interface() noexcept = default; virtual int get() const = 0; }; 类 implementation : public interface { public: int get() const override { return 42; } }; 结构 example { example(std::sharedptr i) { assert(42 == i-get()); } }; int main() { const auto injector = di::makeinjector( di::bind.to() ); injector.create(); } 在 Wandbox 上运行此示例。 Clang-3.8 GCC-6 MSVC-2015 编译时间 0.102s 0.118s 0.296s 二进制大小(去除) 6.2kb 6.2kb 105kb ASM x86-64 (与 makeunique 相同) push %rbx mov %rdi,%rbx mov $0x8,%edi callq 0x4008e0 movq $0x400c78,(%rax) mov %rax,(%rbx) mov %rbx,%rax pop %rbx retq 快速指南 - 绑定模板 … 在 Wandbox 上运行此示例。 Clang-3.8 GCC-6 MSVC-2015 编译时间 0.102s 0.118s 0.296s 二进制大小(去除) 6.2kb 6.2kb 105kb ASM x86-64 xor eax, eax retq 快速指南 - 绑定概念 cpp 结构 Streamable { template auto requires(T&& t) - decltype( int( t.read() ), t.write(int) ); }; 模板类 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::makeinjector(…
暂无开放 Issues,或尚未同步最近议题。