多方面模板类专业化
Multiple aspects template class specialization
>上下文
我目前正在用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
我的问题
我如何确保即使我以Aspect2<DerivedA>
作为模板参数键入它,也会编译Aspect1
的专用化,比如说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。
感谢您的建议!
你不能
,唯一可以确定的方法是是否使用Aspect1<DerivedA>
类型。如果使用,则将其编译,否则不编译。
相关文章:
- 如何使用默认参数等选择模板专业化
- 模板化建造师专业化
- 类模板的成员功能的定义在单独的TU中完全专业化
- 部分专业化和嵌套模板
- 模板专业化可以进入我的.cpp吗?
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 部分专业化和对标准::void_t<>的需求
- "专业化不参与超载"
- 特定好友功能专业化
- 是否可以混合使用SFINAE和模板专业化?
- 为什么在班级专业化上会出现错误?
- enable_if如何帮助选择类模板的专业化?
- std::initializer_list可以专业化吗?
- 派生类中纯虚拟基方法的专业化
- "expected a '>'"类模板专业化?
- Clang不会编译GCC会编译的模板专业化
- 我可以用clang AST从模板专业化中获得默认的模板参数吗
- 函数模板部分专业化-有什么解决方法吗
- 类模板的编译错误,但其专业化除外
- 多方面模板类专业化