Boost-DI with concept-based static polymorphism

11 hours ago 1
ARTICLE AD BOX

I'm using Boost.DI, and I've been having a lot of success learning it. However, I ran into a wrinkle that I can't see exactly how to fix.

The following compiles, links and runs as expected:

class CC { public: void Log(std::string_view) {} }; template<class TC = class C> class Host { TC& c; public: BOOST_DI_INJECT(Host, TC& c) : c(c) { c.Log("moop"); } };

The problem is that there is no concept applied anywhere. I can write the code, and will get compile-time errors, but I'll get no Intellisense code completion because at the time of writing Host it has no idea what that TC is yet. That's especially bad because it's trivial to provide a concept for it:

template<class T> concept IC = requires(T t, std::string_view message) { t.Log(message); };

However trying to apply the concept gives a compile-time error that the concept fails because TC is incomplete:

template<IC TC = class C> // <--- note the concept applied here class Host { TC& c; public: BOOST_DI_INJECT(Host, TC& c) : c(c) { c.Log("moop"); } };

It fails with this error message:

1> (...)\include\boost\di.hpp(2792,103): 1> 'Host': the associated constraints are not satisfied 1> (...)\main.cpp(28,10): 1> the concept 'IC<C>' evaluated to false 1> (...)\main.cpp(19,2): 1> use of undefined type 'C' 1> (...)\main.cpp(28,18): 1> see declaration of 'C' 1> (...)\main.cpp(19,4): 1> left of '.Log' must have class/struct/union

However even while failing, I get my Intellisense as expected now, so it leads me to believe that it is doable:

enter image description here

According to the official documentation, the way concepts are meant to be used are with a branch for not is_complete to allow incomplete types:

template<class T> concept IC = requires(T t, std::string_view message) { t.Log(message); } or not boost::di::aux::is_complete<T>::value;

This again works correctly, and it validates that the function exists at compile time, but Intellisense again fails. As far as I can tell, it just takes the incomplete type branch (perhaps because it's the most permissive?) and so has no idea what to do with my templated parameter:

enter image description here

So, is there a way to make this library work with concept-based polymorphism while allowing Intellisense to also work? Maybe using a modern C++ concept (no pun intended) I'm unfamiliar with?

Read Entire Article