C++惯用法:通过成员模板实现隐式转换(Coercion 强迫 by Member Template)
Intent
To increase the flexibility of a class template's interface by allowing the class template to participate in the same implicit type conversions (coercion) as its parameterizing types enjoy.
Also Known As[edit]
Motivation[edit]
It is often useful to extend a relationship between two types to class templates specialized with those types. For example, suppose that class D derives from class B. A pointer to an object of type D can be assigned to a pointer to B; C++ supports that implicitly. However, types composed of these types do not share the relationship of the composed types. That applies to class templates as well, so a Helper<D> object normally cannot be assigned to a Helper<B> object.
class B {};
class D : public B {};
template <class T>
class Helper {};
B *bptr;
D *dptr;
bptr = dptr; // OK; permitted by C++
Helper<B> hb;
Helper<D> hd;
hb = hd; // Not allowed but could be very useful
There are cases where such conversions are useful, such as allowing conversion from std::auto_ptr<D> to std::auto_ptr<B>. That is quite intuitive, but isn't supported without using the Coercion by Member Template Idiom.
Solution and Sample Code[edit]
Define member template functions, in a class template, which rely on the implicit type conversions supported by the parameter types. In the following example, the templated constructor and assignment operator work for any type U, for which initialization or assignment of a T * from a U * is allowed.
通过在一个类中定义成员模板函数,可以使不同类型的参数得到隐士转换。
template <class T>
class Ptr
{
public:
Ptr () {} Ptr (Ptr const & p)
: ptr (p.ptr)
{
std::cout << "Copy constructor\n";
} // Supporting coercion using member template constructor.
// This is not a copy constructor, but behaves similarly.
template <class U>
Ptr (Ptr <U> const & p)
: ptr (p.ptr) // Implicit conversion from U to T required
{
std::cout << "Coercing member template constructor\n";
} // Copy assignment operator.
Ptr & operator = (Ptr const & p)
{
ptr = p.ptr;
std::cout << "Copy assignment operator\n";
return *this;
} // Supporting coercion using member template assignment operator.
// This is not the copy assignment operator, but works similarly.
template <class U>
Ptr & operator = (Ptr <U> const & p)
{
ptr = p.ptr; // Implicit conversion from U to T required
std::cout << "Coercing member template assignment operator\n";
return *this;
} T *ptr;
}; int main (void)
{
Ptr <D> d_ptr;
Ptr <B> b_ptr (d_ptr); // Now supported
b_ptr = d_ptr; // Now supported
}
http://en.wikibooks.org/wiki/More_C++_Idioms/Coercion_by_Member_Template
C++惯用法:通过成员模板实现隐式转换(Coercion 强迫 by Member Template)的更多相关文章
- C++模板之隐式实例化、显示实例化、隐式调用、显示调用和模板特化详解
模板的实例化指函数模板(类模板)生成模板函数(模板类)的过程.对于函数模板而言,模板实例化之后,会生成一个真正的函数.而类模板经过实例化之后,只是完成了类的定义,模板类的成员函数需要到调用时才会被初始 ...
- Scala 隐式转换及应用
什么是隐式转换 我们经常引入第三方库,但当我们想要扩展新功能的时候通常是很不方便的,因为我们不能直接修改其代码.scala提供了隐式转换机制和隐式参数帮我们解决诸如这样的问题. Scala中的隐式转换 ...
- scala成长之路(3)隐式转换
不废话,先上例子:定义一个参数类型为String的函数: scala> def sayHello(name:String) = println("hello " + name ...
- C++ 不具有继承关系的类之间的显式,隐式转换 2013-07-11 15:41
好久没有写blog了,今天在学习c#的时候看到某一章节 讲类的隐式与显式转换.特此留笔,以供后续参考之用. 关于显式,隐式转换有些争论,说什么不建议隐式转换.但是个人认为非必要,如果有良好的基础书写基 ...
- 显示转换explicit和隐式转换implicit
用户自定义的显示转换和隐式转换 显式转换implicit关键字告诉编译器,在源代码中不必做显示的转型就可以产生调用转换操作符方法的代码. 隐式转换implicit关键字告诉编译器只有当源代码中指定了显 ...
- Scala特性: 隐式转换
1.隐式转换特征: 1)隐式参数的用法 · 获取可能的预期类型 · 获取预期类型,并且拥有预期类型的行为 · 对信息进行补充说明(一般用函数做隐式参数的比较多) 2)隐式类: 3)隐式method:
- 深入理解Scala的隐式转换系统
摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码. 使用方式: 1. ...
- QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)
原作者: Olivier Goffart 点击打开链接http://woboq.com/blog/qstringliteral.html 译者: zzjin 点击打开链接http://www.tuic ...
- F#中的自定义隐式转换
我们知道隐式变换在可控情况下会使代码变得简洁.熟悉C#的都知道C#中可以自定义隐式变换,例如 public class A { private int data; public static impl ...
随机推荐
- linux date -d 的一些使用方法
date命令中格式输出类型字符含义例如以下: %% 一个文字的 % %a 当前locale 的星期名缩写(比如: 日,代表星期日) %A 当前locale 的星期名全称 (如:星期日) %b 当前lo ...
- js判断undefined类型,undefined,null,NaN的区别
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined 所以自作聪明判断 ...
- ios开发学习笔记(1)
objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...
- HDU 4861 Couple doubi(找规律|费马定理)
Couple doubi Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- JavaSE_ 网络编程 目录(26)
JavaSE学习总结第26天_网络编程26.01 网络编程概述26.02 网络模型概述和图解26.03 网络编程三要素概述26.04 网络编程三要素之IP概述126.05 InetAddress类的概 ...
- JavaSE学习总结第26天_网络编程
26.01 网络编程概述 网络编程:就是用来实现网络互连的不同计算机上运行的程序间可以进行数据交换. 26.02 网络模型概述和图解 计算机网络之间以何种规则进行通信,就是网络模型研究问题. ...
- BZOJ 2173: 整数的lqp拆分( dp )
靠着暴力+直觉搞出递推式 f(n) = ∑F(i)f(n-i) (1≤i≤n) (直接想大概也不会很复杂吧...). f(0)=0 感受一下这个递推式...因为和斐波那契有关..我们算一下f(n)+f ...
- 获取执行计划——EXPLAN PLAN
一般获取执行计划有四种途径:1.执行explain plan,查询结果输出表.2.查询动态性能视图,它显示缓存在库缓存中的执行计划(有时查不出结果是因为执行计划已经不在库缓存中).3.查询AWR或St ...
- python函数callable
callable(object) 中文说明:检查对象object是否可调用.如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojbect绝对不会成功. 注意:类或函数是可 ...
- 针对PCB飞针测试快速有效的技巧
测试探针通过多路传输(multiplexing)系统连接到驱动器(信号发生器.电源供应等)和传感器(数字万用表.频率计数器等)来测试UUT上的元件.当一个元件正在测试的时候,UUT上的其它元件通过探针 ...