假设我有这些类型:
template
<
class T,
template <class> class Storage
>
struct AbstractFactoryUnit
{
virtual ~AbstractFactoryUnit() {}
virtual typename Storage< T >::StoredType doCreate(Storage< T >) = 0;
};
和
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 >());
}
};
现在我想实现 le AbstractFactory
...
一些大声笑类型:
struct Foo {};
struct Bar {};
struct Baz {};
大声笑存储:
template <class T>
struct RawPointerStorage
{
typedef T* StoredType;
};
最后是实现:
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;
}
};
不幸的是,编译器抱怨:
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> ]
我有点困惑,因为它只AbstractFactoryUnit
接受一个模板参数时编译得很好。我的猜测是编译器无法“解析”第二个占位符,但我应该承认我不知道为什么——因为我不太清楚 boost 如何调用apply
占位符。
我将 VS2012 与 vc100 或 vc110 一起使用。
任何的想法?(是的,我在玩AbstractFactory
现代 C++ 设计中的描述)
EDIT: I finally decided to provide my whole AbstractFactory
code without disguises in both my question and my answer.