GP随笔: IF模板的一种非偏特化实现
2006-02-17 12:04
232 查看
IF模板一般设计如下:
template <bool /* Condition */, class Then, class /* Else */ >
struct IF {
typedef Then ret;
};
template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else ret;
};
在不支持偏特化的编译器中, 一般使用下面的方式使用全特化模拟:
namespace intimate
{
struct SelectThen
{ template<class Then, class Else>
struct Result
{ typedef Then RET;
};
}; // end SelectThen
struct SelectElse
{ template<class Then, class Else>
struct Result
{ typedef Else RET;
};
}; // end SelectElse
template<bool Condition>
struct Selector
{ typedef SelectThen RET;
}; // end Selector
template<>
struct Selector<false>
{ typedef SelectElse RET;
}; // end Selector<false>
} // end namespace private
template<bool Condition, class Then, class Else>
struct IF
{ typedef intimate::Selector<Condition>::RET select;
typedef select::Result<Then,Else>::RET RET;
}; // IF
上面的实现有点冗长. 最近使用某编译器居然不支持 namespace, 为了不让几个 Selector 污染名字空间, 发现了一种比较简单的实现方案, 有一定的通用性, 可用于将模板代码移植到不支持偏特化或名字空间的情况.
template <bool Condition , class Then, class Else>
class IF {
template<bool /* Condition */ >
struct Selector {
typedef Then ret;
};
template<>
struct Selector<false> {
typedef Else ret;
};
public:
typedef Selector<Condition>::ret ret;
};
实现技术本质上没有多大变化, 区别无非是使用嵌套类的外层类的private域充当名字空间.
使用全特化模拟偏特化, 基本原理是分离出那些与特化无关的模板参数.
在这种实现里面, 用外层类向嵌套类传递与特化无关的模板参数, 而由嵌套类实现特定模板参数的全特化.
最后在public域输出特化结果.
template <bool /* Condition */, class Then, class /* Else */ >
struct IF {
typedef Then ret;
};
template<class Then, class Else>
struct IF<false, Then, Else> {
typedef Else ret;
};
在不支持偏特化的编译器中, 一般使用下面的方式使用全特化模拟:
namespace intimate
{
struct SelectThen
{ template<class Then, class Else>
struct Result
{ typedef Then RET;
};
}; // end SelectThen
struct SelectElse
{ template<class Then, class Else>
struct Result
{ typedef Else RET;
};
}; // end SelectElse
template<bool Condition>
struct Selector
{ typedef SelectThen RET;
}; // end Selector
template<>
struct Selector<false>
{ typedef SelectElse RET;
}; // end Selector<false>
} // end namespace private
template<bool Condition, class Then, class Else>
struct IF
{ typedef intimate::Selector<Condition>::RET select;
typedef select::Result<Then,Else>::RET RET;
}; // IF
上面的实现有点冗长. 最近使用某编译器居然不支持 namespace, 为了不让几个 Selector 污染名字空间, 发现了一种比较简单的实现方案, 有一定的通用性, 可用于将模板代码移植到不支持偏特化或名字空间的情况.
template <bool Condition , class Then, class Else>
class IF {
template<bool /* Condition */ >
struct Selector {
typedef Then ret;
};
template<>
struct Selector<false> {
typedef Else ret;
};
public:
typedef Selector<Condition>::ret ret;
};
实现技术本质上没有多大变化, 区别无非是使用嵌套类的外层类的private域充当名字空间.
使用全特化模拟偏特化, 基本原理是分离出那些与特化无关的模板参数.
在这种实现里面, 用外层类向嵌套类传递与特化无关的模板参数, 而由嵌套类实现特定模板参数的全特化.
最后在public域输出特化结果.
相关文章推荐
- 一种在编译期保证模板类参数T必须实现某函数的方法
- 从模板引擎到模板引擎-使用aspx页面作为模板引擎的一种实现
- 模板引擎的一种实现
- 【C++】单例模式的一种简单实现的模板
- 使用boost::enable_if与boost::type_traits在模板中实现对模板类型的接口的限制
- asp.net从模板引擎到模板引擎-使用aspx页面作为模板引擎的一种实现
- 模板引擎的一种实现-aspx页面的实现
- Aspx页面作为最好的模板引擎一种实现
- 使用aspx页面作为模板引擎的一种实现
- 实现文件拖放的一种简洁方法
- 使用.net core ABP和Angular模板构建博客管理系统(实现自己的业务逻辑)
- 使用GridView的模板列实现在线编辑的功能
- 一种基于delphi巧妙权限控制方案及其实现方法
- JAVA自定义异常类的一种实现方式
- (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画
- Struts,MVC 的一种开放源码实现
- 基于边界的模板匹配的原理及算法实现
- 设计模式C++实现(5)——原型模式、模板方法模式
- freemarker实现通用布局的模板拆分与复用
- C++语言机制之多态性 与 代码重用机制的一种工具之模板 小结