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)的更多相关文章

  1. C++模板之隐式实例化、显示实例化、隐式调用、显示调用和模板特化详解

    模板的实例化指函数模板(类模板)生成模板函数(模板类)的过程.对于函数模板而言,模板实例化之后,会生成一个真正的函数.而类模板经过实例化之后,只是完成了类的定义,模板类的成员函数需要到调用时才会被初始 ...

  2. Scala 隐式转换及应用

    什么是隐式转换 我们经常引入第三方库,但当我们想要扩展新功能的时候通常是很不方便的,因为我们不能直接修改其代码.scala提供了隐式转换机制和隐式参数帮我们解决诸如这样的问题. Scala中的隐式转换 ...

  3. scala成长之路(3)隐式转换

    不废话,先上例子:定义一个参数类型为String的函数: scala> def sayHello(name:String) = println("hello " + name ...

  4. C++ 不具有继承关系的类之间的显式,隐式转换 2013-07-11 15:41

    好久没有写blog了,今天在学习c#的时候看到某一章节 讲类的隐式与显式转换.特此留笔,以供后续参考之用. 关于显式,隐式转换有些争论,说什么不建议隐式转换.但是个人认为非必要,如果有良好的基础书写基 ...

  5. 显示转换explicit和隐式转换implicit

    用户自定义的显示转换和隐式转换 显式转换implicit关键字告诉编译器,在源代码中不必做显示的转型就可以产生调用转换操作符方法的代码. 隐式转换implicit关键字告诉编译器只有当源代码中指定了显 ...

  6. Scala特性: 隐式转换

    1.隐式转换特征: 1)隐式参数的用法 · 获取可能的预期类型 · 获取预期类型,并且拥有预期类型的行为 · 对信息进行补充说明(一般用函数做隐式参数的比较多) 2)隐式类: 3)隐式method:

  7. 深入理解Scala的隐式转换系统

    摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码.   使用方式: 1. ...

  8. QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)

    原作者: Olivier Goffart 点击打开链接http://woboq.com/blog/qstringliteral.html 译者: zzjin 点击打开链接http://www.tuic ...

  9. F#中的自定义隐式转换

    我们知道隐式变换在可控情况下会使代码变得简洁.熟悉C#的都知道C#中可以自定义隐式变换,例如 public class A { private int data; public static impl ...

随机推荐

  1. eclipse svn插件显示作者

    在另一台电脑里安装了SVN插件后,发现项目文件后面只有版本号,没有作者名字了,找了很久才找到了,现记录在这里. window->preferences->team->svn-> ...

  2. unix more命令

    [语法]: more   [-cdflrsuw]  [- 行数] [+ 行数] [+ / 模式 ] [ 文件 ... ] [说明]: 将文件显示在终端上.每次一屏,在左下部显示 --more--.若是 ...

  3. C++内联函数与宏定义

    用内联取代宏: 1.内联可调试: 2.可进行类型安全检查或自动类型转换: 3.可访问成员变量. 另外,定义在类声明中的成员函数自动转化为内联函数. 文章(一) 内联函数与宏定义 在C中,常用预处理语句 ...

  4. BZOJ 1483: [HNOI2009]梦幻布丁( 链表 + 启发式合并 )

    把相同颜色的串成一个链表, 然后每次A操作就启发式合并, 然后计算对答案的影响. ----------------------------------------------------------- ...

  5. Linux 网络编程: echo Service

    前言 大病初愈,感谢某人的陪伴,感谢王乐庆同学和赵攀同学的细心照顾.原以为过了第八周就不忙了,却没想到还有明天的党章考试.还是写代码比背党章有意思~趁着服务器还没过期,赶紧把 echo 完成了.关于错 ...

  6. docker4dotnet

    docker4dotnet #1 – 前世今生 & 世界你好   作为一名.NET Developer,这几年看着docker的流行实在是有些眼馋.可惜的是,Docker是基于Linux环境的 ...

  7. qt 国际化(翻译时会触发changeEvent)

    1. 修改工程文件 .pro ,加入翻译源文件 hello_world.ts: TRANSLATIONS += \        Resource/translations/hello_world.t ...

  8. Node.Buffer

    介绍 Buffer是一个典型的javascript与c++结合的模块,它将性能相关的部分用c++实现,将非性能相关的部分用javascript实现. 纯 JavaScript 对 Unicode 友好 ...

  9. PE框架学习之道:PE框架——发送报文流程

    PE框架发送报文,适用于PE及VX技术 步骤: 1.在action中使用发送报文,要指定报文在router端的交易名称 2.如果使用supe.execute(context)来发送,不需要第一步 3. ...

  10. Jquery Select 下拉框处理

    $("#select").empty();//清空 $("#select").append($("<option/>").val ...