How to properly configure boost :: mpl :: inherit_linearly and boost :: mpl :: inherit so that placeholders can be resolved?

Let's say I have the following types:

template < class T, template <class> class Storage > struct AbstractFactoryUnit { virtual ~AbstractFactoryUnit() {} virtual typename Storage< T >::StoredType doCreate(Storage< T >) = 0; }; 

and

 template < class TypeSequence, template <class> class ProductStorage, template <class, template <class> class> class Unit = AbstractFactoryUnit > struct AbstractFactory : boost::mpl::inherit_linearly < TypeSequence, boost::mpl::inherit < boost::mpl::_1, Unit< boost::mpl::_2, ProductStorage > > >::type { typedef TypeSequence Products; template <class T> auto create() -> typename ProductStorage< T >::StoredType { Unit< T, ProductStorage >& unit = *this; unit.doCreate(ProductStorage< T >()); } }; 

Now I want to implement le AbstractFactory ...

Some types of lol:

 struct Foo {}; struct Bar {}; struct Baz {}; 

Repository lol:

 template <class T> struct RawPointerStorage { typedef T* StoredType; }; 

and finally, the implementation:

 struct FooBarBaz : AbstractFactory< boost::mpl::set< Foo, Bar, Baz >, RawPointerStorage > { A* doCreate(RawPointerStorage< Foo >) override { return new A; } B* doCreate(RawPointerStorage< Bar >) override { return new B; } C* doCreate(RawPointerStorage< Baz >) override { return new C; } }; 

Unfortunately, the compiler complains:

 1>C:\Libs\boost\boost_1_51_0\boost/mpl/aux_/preprocessed/plain/inherit.hpp(20): error C2500: 'boost::mpl::inherit2<T1,T2>' : 'AbstractFactoryUnit<T,ProductStorage>' is already a direct base class 1> with 1> [ 1> T1=AbstractFactoryUnit<boost::mpl::_2,RawPointerStorage>, 1> T2=AbstractFactoryUnit<boost::mpl::_2,RawPointerStorage> 1> ] 1> and 1> [ 1> T=boost::mpl::_2, 1> ProductStorage=RawPointerStorage 1> ] 

I'm a little confused as it compiles just fine when AbstractFactoryUnit accepts only one template parameter. My assumption is that the compiler cannot “resolve” the second placeholder, but I have to admit that I don’t know why - because I don’t know how boost calls apply on placeholders.

I am using VS2012 with vc100 or vc110.

Any idea? (yes, I played with AbstractFactory described in modern C ++ design)

EDIT : I finally decided to provide all the AbstractFactory code without disguise in both my question and my answer.

+6
source share
1 answer

I don’t know exactly why in this context the second placeholder cannot be "expanded", but I found that wrapping the expression boost::mpl::inherit solved my problem.

So, here, this is AbstractFactory in a nutshell:

We encapsulate the implementation in the Impl namespace:

 namespace Impl { template < class TypeSequence, template <class> class ProductStorage, template <class, template <class> class> class Unit > struct AbstractFactory { private: template <class T, class U> struct Inherit : boost::mpl::inherit< T, Unit< U, ProductStorage > > {}; public: typedef typename boost::mpl::inherit_linearly < TypeSequence, // the trick is on the following line Inherit< boost::mpl::_1, boost::mpl::_2 > > ::type Type; }; } // namespace Impl 

and we get from it like this:

 template < class TypeSequence, template <class> class ProductStorage = RawPointerStorage, template <class, template <class> class> class Unit = AbstractFactoryUnit > struct AbstractFactory : Impl::AbstractFactory< TypeSequence, ProductStorage, Unit >::Type { typedef TypeSequence Products; template <class T> auto create() -> typename ProductStorage< T >::StoredType { Unit< T, ProductStorage >& unit = *this; return unit.doCreate(ProductStorage< T >()); } }; 
+1
source

All Articles