多方面模板类专业化

时间:2013-03-24 15:33:50

标签: c++ templates template-specialization aop

上下文

我目前正在用C ++编写一些面向方面的代码。我有以下类层次结构:

class Base                   { virtual void doSmth() {/* generic stuff */ } };
class DerivedA : public Base { virtual void doSmth() {/* specific DerivedA stuff */} };
class DerivedB : public Base { virtual void doSmth() {/* specific DerivedB stuff */} };

我已经定义了一些方面类来为上述类添加一些功能:

template <class I>
class Aspect1 : public I
{
  void doSmth() { I::doSmth(); doSmthMore(); }
  void doSmthMore() { /* do something specific to Aspect1 */ }
};


template <class I>
class Aspect2 : public I
{
  void doSmth() { I::doSmth(); doSmthMore(); }
  void doSmthMore() { /* do something specific to Aspect2 */ }
};

在特定情况下(Aspect1超过DerivedADerivedB),我对Aspect1进行了以下专业化处理:

template <>
class Aspect1< DerivedA > : public DerivedA
{
  void doSmth() { I::doSmth(); doSmthMore(); doSmthMoreA(); }
  void doSmthMore()  { /* do something specific to Aspect1 */ }
  void doSmthMoreA() { /* do something specific to Aspect1 for DerivedA*/ }
};
// Idem for Aspect1//DerivedB

我的问题

即使我使用Aspect1作为模板参数输入DerivedA,我怎么能确定编译Aspect2<DerivedA>的特殊化,即typedef Aspect2<Aspect1<DerivedA> > > ClassToBeUsed; // This is OK typedef Aspect1<Aspect2<DerivedA> > > ClassToBeUsed; // This is not (Aspect1 specialization for DerivedA is not compiled)

即。在配置文件中:

boost::is_base_of

一种可能性是衍生自DerivedA的任何类都使用特化Aspect1。有没有办法做到这一点(可能使用一些boost::enable_iftypedef DerivedA AspectBase;)?

我认为我可以在DerivedA正文中使用一些Aspect1,但我没有看到如何在模板类参数中的typedef上专门化{{1}}模板类。

感谢您的建议!

1 个答案:

答案 0 :(得分:0)

您不能,唯一可以确定的是使用类型Aspect1<DerivedA>。如果使用它,它将被编译,否则不会。