di icon indicating copy to clipboard operation
di copied to clipboard

Inject std::vector<std::shared_ptr<T>>

Open KilianBl opened this issue 1 year ago • 0 comments

Expected Behavior

di.hpp should not cause compilation errors or inject empty vectors

Actual Behavior

boost::di::bind<Base*[]>().to({std::make_shared<Derived>(1), std::make_shared<Derived>(2)}) -> cannot pass initializer list to variadic method compilation error

boost::di::bind<Base*[]>().to(vec) -> an empty vector is injected

Steps to Reproduce the Problem

#include <iostream>

#include "di.hpp"

struct Base {
};

struct Derived : Base {
    explicit Derived(const int integer) {
        std::cout << integer << std::endl;
    }
};

struct Test {
    explicit Test(std::vector<std::shared_ptr<Base>> vec) {
        std::cout << vec.size() << std::endl;
    }
};

int main() {
    std::vector<std::shared_ptr<Base>> vec{std::make_shared<Derived>(1), std::make_shared<Derived>(2)};

    const auto injector = boost::di::make_injector(
        // boost::di::bind<Base *[]>().to({std::make_shared<Derived>(1), std::make_shared<Derived>(2)}),
        boost::di::bind<Base *[]>().to(vec));

    std::ignore = injector.create<Test>();

    return 0;
}

Am I doing something wrong? Is there any alternative to get what I need?

Specifications

  • Version: 1.3.0
  • Platform: macOS
  • Compiler: Apple clang version 14.0.3 (clang-1403.0.22.14.1)

KilianBl avatar Oct 22 '24 08:10 KilianBl