zoukankan      html  css  js  c++  java
  • chapter2 techniques 2.2 partial template specialization

    template <class Window, class Controller>
    class Widget
    {
       ... generic implementation ...
    };

    template <>
    class Widget<ModalDialog, MyController>
    {
       ... specialized implementation ...
    };

    // Partial specialization of Widget
    template <class Window>
    class Widget<Window, MyController>
    {
       ... partially specialized implementation ...
    };

    Unfortunately, partial template specialization does not apply to functions—be they member or
    nonmember—which somewhat reduces the flexibility and the granularity of what you can do.

    偏特化不能用于函数,全局的或是成员的。
    •  Although you can totally specialize member functions of a class template, you cannot partially
    specialize member functions.

    你可以全特化一个成员函数,但是不能偏特化
    •  You cannot partially specialize namespace-level (nonmember) template functions. The closest
    thing to partial specialization for namespace-level template functions is overloading. For practical
    purposes, this means that you have fine-grained specialization abilities only for the function
    parameters—not for the return value or for internally used types. For example,
    template <class T, class U> T Fun(U obj); // primary template
    template <class U> void Fun<void, U>(U obj); // illegal partial
    //        specialization
    template <class T> T Fun (Window obj); // legal (overloading)

    你不能偏特化一个全局的函数。但是可以重载全局函数。这说明只能控制函数参数,但是不能控制返回值和内部使用的类型。


    This lack of granularity in partial specialization certainly makes life easier for compiler writers, but has
    bad effects for developers. Some of the tools presented later (such as Int2Type and Type2Type)
    specifically address this limitation of partial specialization.

    可以通过Int2Type and Type2Type等弥补。

  • 相关阅读:
    Yii2的相关学习记录,后台模板和gii(三)
    Yii2的相关学习记录,初始化Yii2(二)
    Yii2的相关学习记录,下载Yii2(一)
    虚拟机上网总结
    纯Java实现微信朋友圈分享图
    【集合框架】JDK1.8源码分析之ArrayList详解(一)
    Java中字符串相加和字符串常量相加区别
    onTouchEvent中,跟随手指滑动的view出现抖动
    Android CHM文件阅读器
    CHM格式
  • 原文地址:https://www.cnblogs.com/cute/p/2142847.html
Copyright © 2011-2022 走看看