[Boost].DI template injection does not work as expected

Hi,

The following code does not compile. It was obtained from https://boost-experimental.github.io/di/FAQ/index.html#can-i-inject-templatesconcepts.
It is supposed to be a very simple example of injecting templates with the DI library. I have tried everything to no avail. Any help will be greatly appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T = class Greater>
struct example { 
  using type = T;
};

struct hello {};

int main() {
  const auto injector = di::make_injector(
    di::bind<class Greater>().to<hello>()
  );

  auto object = injector.create<example>();
  static_assert(std::is_same<hello, decltype(object)::type>{});
}


Regards,
Juan
It would be nice if you could tell us why it does not compile, we can't read minds.
What's the errors you get?
No need to read minds: hoover the cursor over the symbol near the upper right corner of the code window and press the left mouse button, this will open www.cpp.sh with the code in question. Hit "Run" and see
 In function 'int main()':
9:25: error: 'di' has not been declared
10:5: error: 'di' has not been declared
10:14: error: expected primary-expression before 'class'
13:40: error: missing template arguments before '>' token
13:42: error: expected primary-expression before ')' token
14:17: error: 'is_same' is not a member of 'std'
14:35: error: expected primary-expression before ',' token
14:37: error: expected string-literal before 'decltype'
14:37: error: expected ')' before 'decltype'
It compiles fine with GCC 8.3.0 and Clang.

GCC 9, OTOH, produces (among others) the somewhat disheartening diagnostic message
error: wrong number of template arguments (1, should be at least 1)

I suspect the problem is a symptom of a compiler bug.

Try it on Wandbox:
https://wandbox.org/permlink/WAGWoBng1L8djJNG
Last edited on
Sorry, code was:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <boost/di.hpp>

	namespace di = boost::di;

	template <class T = class Greater>  
	struct example {
		using type = T;
	};

	struct hello {};

	void main() {
		const auto injector = di::make_injector(
			di::bind<class Greater>().to<hello>()
		);

		auto object = injector.create<example>();
		static_assert(std::is_same<hello, decltype(object)::type>::value);
		int i = 0;
	}


From https://boost-experimental.github.io/di/FAQ/index.html#can-i-inject-templatesconcepts

Can you compile it now?
I am using Visual Studio 2019
Poking around the documentation (and using a bit of luck) it appears that this experimental boost header 3rd party library doesn't support VS2019 yet. Only 2015 and 2017.

Or it does. VS2019 and 2017 choke on the static_assert line. Have to enable C++17 or higher language support in both to use the single parameter static_assert.

Once the language support is set the static_assert fails.

VS2017 doesn't like injector.create.
Last edited on
Can you compile it now?
Sorry, no. I just use the service offered on www.cpp.sh and it moans about

#include <boost/di.hpp>

1:24: fatal error: boost/di.hpp: No such file or directory
compilation terminated.

The only thing I may see is, void main() should be int main() due to an implicit return 0 (the same mistake I made myself recently and someone on this list was so kind and advised me to correct it. More here: http://www.cplusplus.com/doc/tutorial/functions/#mainreturns )

Next, in line 19 you set variable i but never use it again.

Not of much help, I know. But almost nothing is much more than absolutely nothing.
For the include failure, just download from https://github.com/boost-experimental/di
what compiler are you using? I use VStudio 2019

Regards,
Juan Dent
For the include failure, just download...

That won't work for an most online compilers that I know of.
Last edited on
Matt Godbolt's Compiler Explorer allows inclusion of web URIs...:
https://godbolt.org/z/VvG_aL
Matt Godbolt's Compiler Explorer allows inclusion of web URIs...

Downloading a header file to one's computer won't help, though.

Nice to see one online compiler can deal with 3rd party library headers if they can be accessed via a URL.
Last edited on
Downloading a header file to one's computer won't help, though.

It should be fine in this case - the entire library is one header file.
Last edited on
Topic archived. No new replies allowed.