zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 41: Understand implicit interfaces and compiletime polymorphism.

    Look what happens when we turn doProcessing from a function into a function template:

    void doProcessing(Widget& w)
    {
        if (w.size() > 10 && w != someNastyWidget) 
        {
            Widget temp(w);
            temp.normalize();
            temp.swap(w);
        }
    }
    
    template<typename T>
    void doProcessing(T& w)
    {
        if (w.size() > 10 && w != someNastyWidget) 
        {
            T temp(w);
            temp.normalize();
            temp.swap(w);
        }
    }

    Now what can we say about w in doProcessing?

    • The interface that w must support is determined by the operations performed on w in the template. In this example, it appears that w's type (T) must support the size, normalize, and swap member functions; copy construction (to create temp); and comparison for inequality (for comparison with someNastyWidget). We'll soon see that this isn’t quite accurate, but it's true enough for now. What’s important is that the set of expressions that must be valid in order for the template to compile is the implicit interface that T must support.
    • The calls to functions involving w such as operator> and operator!= may involve instantiating templates to make these calls succeed. Such instantiation occurs during compilation. Because instantiating function templates with different template parameters leads to different functions being called, this is known as compile-time polymorphism.

    An explicit interface typically consists of function signatures, i.e., function names, parameter types, return types, etc. The Widget class public interface, for example,

    class Widget 
    {
    public:
        Widget();
        virtual ~Widget();
        virtual std::size_t size() const;
        virtual void normalize();
        void swap(Widget& other);
    };

    An implicit interface is quite different. It is not based on function signatures. Rather, it consists of valid expressions. Look again at the conditional at the beginning of the doProcessing template:

    template<typename T>
    void doProcessing(T& w)
    {
        if (w.size() > 10 && w != someNastyWidget)
        {
            // ...
        }
    
        // ...
    }

    Things to Remember:

    • Both classes and templates support interfaces and polymorphism.
    • For classes, interfaces are explicit and centered on function signatures. Polymorphism occurs at runtime through virtual functions.
    • For template parameters, interfaces are implicit and based on valid expressions. Polymorphism occurs during compilation through template instantiation and function overloading resolution.
  • 相关阅读:
    oracle拼接函数:将多个字段拼接在一行显示
    Source Insight 自定义命令说明
    harview .har文件解析
    GSM设备和网络错误代码
    mknod 创建设备
    linux下的usb抓包方法
    一些函数
    vmware 软件打开 自动开启虚拟机(快捷方式)
    Unix下C程序内存泄漏检测工具Valgrind安装与使用
    windows vmware 系统自启动
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15665716.html
Copyright © 2011-2022 走看看