zoukankan      html  css  js  c++  java
  • plain old C++ functions, base模板函数与特化的模板函数

    非常好的一篇文章http://www.gotw.ca/publications/mill17.htm

    template <typename T>
    void call_who(T) {
        std::cout << "base templates T called" << std::endl;
    };
    
    template <typename T>
    void call_who(T*) {
        std::cout << "base templates T* called" << std::endl;
    }
    
    template <>
    void call_who(int*) {
        std::cout << "specialized int* called" << std::endl;
    };
    
    int main() {
        int a = 0;
        call_who(&a);
            return 0;
    }

    一看就知道调用的是void call_who(int*)。可是,

    template <typename T>
    void call_who(T) {
        std::cout << "base templates T called" << std::endl;
    };
    
    template <>
    void call_who(int*) {
        std::cout << "specialized int* called" << std::endl;
    };
    
    template <typename T>
    void call_who(T*) {
        std::cout << "base templates T* called" << std::endl;
    }
    
    int main() {
    
        int a = 0;
        call_who(&a);
            return 0;
    }

    竟然调用的是: void call_who(T*)。!!!!

    原因是文章中提到的:

    Finally, let's focus on function templates only and consider the overloading rules to see which ones get called in different situations. The rules are pretty simple, at least at a high level, and can be expressed as a classic two-class system:

    Nontemplate functions are first-class citizens. A plain old nontemplate function that matches the parameter types as well as any function template will be selected over an otherwise-just-as-good function template.

    If there are no first-class citizens to choose from that are at least as good, then function base templates as the second-class citizens get consulted next. Which function base template gets selected depends on which matches best and is the "most specialized" (important note: this use of "specialized" oddly enough has nothing to do with template specializations; it's just an unfortunate colloquialism) according to a set of fairly arcane rules:

    If it's clear that there's one "most specialized" function base template, that one gets used. If that base template happens to be specialized for the types being used, the specialization will get used, otherwise the base template instantiated with the correct types will be used.

    Else if there's a tie for the "most specialized" function base template, the call is ambiguous because the compiler can't decide which is a better match. The programmer will have to do something to qualify the call and say which one is wanted.

    Else if there's no function base template that can be made to match, the call is bad and the programmer will have to fix the code.

  • 相关阅读:
    python 抓取网页
    Vim XDebug调试PHP php远程调试
    10 条 nmap 技巧
    Linux修改文件及文件夹权限
    mysql 常用命令 汇总
    VS2010打开过多的IntelliTrace.exe进程导致虚拟内存不足的解决办法
    黄聪:MYSQL远程连接失败:ERROR 1130: mysql 1130连接错误的有效解決方法
    黄聪:WordPress搬家更换域名教程
    黄聪:使用 ALinq 实现 Linq to MySQL【转】
    黄聪:Filezilla 二进制上传设定
  • 原文地址:https://www.cnblogs.com/redips-l/p/11643821.html
Copyright © 2011-2022 走看看