【发布时间】:2013-03-24 15:33:49
【问题描述】:
上下文
我目前正在用 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 超过 DerivedA 和 DerivedB)我有以下 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)
一种可能性是从 DerivedA 派生的任何类都使用特化 Aspect1 。有没有办法做到这一点(也许使用一些boost::is_base_of 和boost::enable_if)?
我认为我可以在 DerivedA 正文中使用一些 typedef DerivedA AspectBase;,但我不知道如何将 Aspect1 模板类专门化为模板类参数中的 typedef。
感谢您的建议!
【问题讨论】:
标签: c++ templates template-specialization aop