zoukankan      html  css  js  c++  java
  • 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
    ";
        }
     
        // 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
    ";
        }
     
        // Copy assignment operator.
        Ptr & operator = (Ptr const & p)
        {
          ptr = p.ptr;
          std::cout << "Copy assignment operator
    ";
          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
    ";
          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

  • 相关阅读:
    Android客户端消息推送原理简介
    优秀程序员的十个习惯
    能让你成为更优秀程序员的10个C语言资源
    成大事必备9种能力、9种手段、9种心态
    33个优秀的HTML5应用演示 (转)
    Maven学习:Eclipse使用maven构建web项目(转)
    使用fiddler模拟http get
    TLS握手
    风暴英雄 http 302重定向 正在等待游戏模式下载完成
    page template in kentico
  • 原文地址:https://www.cnblogs.com/youxin/p/3280303.html
Copyright © 2011-2022 走看看