Closed jamespharvey20 closed 4 years ago
Found that this works:
di::bind<string[]>.to<string>({"123", "456", "789"})
I'm leaving this open, because I don't know if any of the things I originally posted should work, or if there should be more clear errors given. Also, because I don't know if the solution I found is the best/minimal way to do this.
It would be nice to have an in-depth explanation of the make_injector
and bind
syntax, in a way as close as possible to "explain it to me like I'm 5". Occasionally I wind up stuck trying a bunch of things until it works, and would love to have the knowledge to just know how to write it correctly.
I've seen https://boost-experimental.github.io/di/user_guide/index.html#di_make_injector and https://boost-experimental.github.io/di/user_guide/index.html#di_bind but don't have a great grasp on things like these:
bind<XXX>
template as the type argument (i.e., here, using c []
arrays rather than vectors -- why, and when else is using a "less-typed" type required?)class
needs to be given in the template type argument. It's in example/bind_templates
and example/try_it
and I can remove these and compile successfully. But, it's also in ft/di_bind
which fails without it. I think it's only required when binding to a forward declaration, and accidentally in the examples I listed.()
immediately after bind<XXX>
. It's in most of the examples and tests, but doesn't seem to be needed. (Maybe some compilers?)to
.to
. Think it's whenever binding to an array-type and creating more than one element in the array.You can also try di::bind<string[]>.to<string>({"123"s, "456"s, "789"s})
. Ultimately, "123" has const char[4]
type and not string
. You need to tell DI what you mean by this binding explicitly.
Bonus question: is there a way to specify vector
rather than T[]? No you cannot. But it is just an implementation detail. You shouldn't worry about it being an array rather than a vector
Expected Behavior
To be able to inject into a constructor taking a
vector<string>
.Actual Behavior
Fails compilation, or constructs an empty vector.
Steps to Reproduce the Problem
For
vector<int>
, this example works:Notably, it has to be done that way as shown in your examples.
di::bind<vector<int>>
causes a compilation failure. (Bonus question: is there a way to specifyvector<T>
rather thanT[]
?)And, as expected the above example, prints:
But, I'm having trouble getting this to work with a constructor taking
vector<string>
:If I try this:
I get these compilation and linker errors:
If I try this:
Then it compiles, but prints nothing, because for some reason the constructed has size 0.
Same with:
If I try this:
I get this compilation error:
If I split it to:
Then it compiles, but prints nothing, because for some reason the constructed has size 0. (With compilation, using
-Wwrite-strings
, I get awarning: ISO C++ forbids converting a string constant to 'char*'
because the string literals should be const, so I'm looking for a solution that doesn't trigger this warning anyway.)If I try:
Then I get: (basically the same error as if I try
vector<int>
in the original example.If I try:
Then it still constructs an empty vector. (I have verified in
gdb
that I canprint list
and properly see its contents.)Also, if I mess up and miss the braces:
I get this linker error. Maybe there's an improvement here, maybe that's just on me for missing the braces:
I've tried about 50 other things, and won't bother you with posting all the errors and what went wrong with them. Haven't been able to find one that works.
Specifications