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等弥补。

  • 相关阅读:
    关于CLR、CIL、CTS、CLS、CLI、BCL和FCL
    (DateTime)日期型数据转换成C#长整型数据
    List<T>.FindIndex 方法 (Predicate<T>)
    C#用int的0--31位表示32个bool值,int拆分成bool数组
    C# 协程 WaitForSeconds产生GC(Garbage Collection)问题
    Unity3D教程:尽量避免使用foreach
    NGUI中button无法用find函数找到
    Vue + axios + SpringBoot 2实现导出Excel
    Kafka 0.10.1版本源码 Idea编译
    Gradle Wrapper
  • 原文地址:https://www.cnblogs.com/cute/p/2142847.html
Copyright © 2011-2022 走看看