template <class T = class Greater>
struct example {
using type = T;
};
struct hello {};
int main() {
constauto injector = di::make_injector(
di::bind<class Greater>().to<hello>()
);
auto object = injector.create<example>();
static_assert(std::is_same<hello, decltype(object)::type>{});
}
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'
#include <boost/di.hpp>
namespace di = boost::di;
template <class T = class Greater>
struct example {
using type = T;
};
struct hello {};
void main() {
constauto 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;
}
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.
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.