zoukankan      html  css  js  c++  java
  • integral_constant

    // from c++11 standard
    namespace std {
    template <class T, T v>
    struct integral_constant {
    static constexpr T value = v;
    typedef T value_type;
    typedef integral_constant<T,v> type;
    constexpr operator value_type() { return value; }
    };
    typedef integral_constant<bool, true> true_type;
    typedef integral_constant<bool, false> false_type;
    }

    ————————————————
    版权声明:本文为CSDN博主「csfreebird」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/csfreebird/article/details/44904121

    #include <iostream>
    #include <type_traits>

    using namespace std;

    //实现求阶乘
    template<unsigned n>
    struct fac : std::integral_constant<int, n*fac<n - 1>::value>{};
    template<>
    struct fac<0> : std::integral_constant<int, 1>{};


    int main()
    {
    cout << fac<5>::value << endl;
    return 0;
    }

  • 相关阅读:
    函数嵌套
    函数对象
    可变长参数
    函数的参数
    函数的调用
    函数的返回值
    定义函数的三种形式
    函数的定义
    SQLAlchemy
    Flask总结完整版
  • 原文地址:https://www.cnblogs.com/xpylovely/p/12205375.html
Copyright © 2011-2022 走看看