zoukankan      html  css  js  c++  java
  • 函数模板

     拖尾返回类型:当模板函数的返回值取决于特定的模板形参时,可以使用 auto 关键字指定返回类型,然后在函数头的 -> 后使用 decltype 操作符来定义返回类型。

    template<class T1,class T2>
    auto product(T1 a[], T2 b[], int count)->decltype(a[0] * b[0])
    {
        decltype(a[0] * b[0]) sum {};
        for (int i = 0; i < count; ++i)
        {
            sum += a[i] * b[i];
        }
        return sum;
    }
    
    int main(int argc, char* argv[])
    {
        int a[] = { 1, 2, 3 };
        long b[] = { 1, 2, 3 };
        int n = sizeof(a) / sizeof(a[0]);
        auto ret = product(a, b, n); //14
        const char* s = typeid(product(a, b, n)).name(); //long
        return 0;
    }

    decltype(..)是获得一个表达式的结果值的类型。->后的是函数的返回类型。

    常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。

    昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
  • 相关阅读:
    kill一个pthread_test.bin测试程序主线程、子线程退出kernel flow
    signal bit operation
    pthread
    信号发送处理流程
    sdcardfs
    node小贴士03
    node小贴士02
    node小贴士01
    siteserver cms 搜索功能
    语法的高亮显示
  • 原文地址:https://www.cnblogs.com/htj10/p/11439260.html
Copyright © 2011-2022 走看看