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.
  • 相关阅读:
    tensorflow 计算图模型的保存和恢复
    Python 小项目
    一些Python代码
    Anaconda 创建的新环境的位置, 解决pycharm中不能访问numpy的问题, conda命令 、设置镜像源、解决jupyter启动新环境kernel问题
    gdb调试程序
    一个完整的Makefile文件举例
    机器学习 Coursera Note
    2018年第九届蓝桥杯 第九题:全球变暖(满分23分)
    2018年第九届蓝桥杯 第八题:日志统计(满分21分)
    2018年第九届蓝桥杯 第七题:螺旋折线(满分19分)
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15665716.html
Copyright © 2011-2022 走看看