有关模板元编程的几个有趣的例子

阶乘

#include <cstdint>

template<uint64_t N>
struct Fact
{
    enum { Value = N * Fact<N - 1>::Value };
};

template<>
struct Fact<1ULL>
{
    enum { Value = 1ULL };
};

斐波那契额数列

template<uint64_t N>
struct Fib
{
    enum { Value = Fib<N - 1>::Value + Fib<N - 2>::Value };
};

template<>
struct Fib<2ULL>
{
    enum { Value = 1ULL };
};
template<>
struct Fib<1ULL>
{
    enum { Value = 1ULL };
};

乘方运算

template<int64_t base, uint64_t exp>
struct Pow
{
    enum { Value = base * Pow<base, exp-1>::Value };
};

template<int64_t base>
struct Pow<base, 1>
{
    enum { Value = base };
};

template<int64_t base>
struct Pow<base, 0>
{
    enum { Value = 1 };
};

测试

#include <iostream>

using ::std::cout;
using ::std::endl;

int main(int argc, char const *argv[])
{
    cout << Fact<5ULL>::Value << endl;
    cout << Fib<10ULL>::Value << endl;
    cout << Pow<2, 10>::Value << endl;
    return 0;
}

运行结果如下

120
403200
1024

新的写法

上述代码是C++11以前的写法,借助了enum在编译期求值的特性。C++11以后可使用static constexpr变量取代。具体做法如下:

#include <cstdint>

template<uint64_t N>
struct Fact
{
    static constexpr uint64_t Value = N * Fact<N - 1>::Value;
};

template<>
struct Fact<1ULL>
{
    static constexpr uint64_t Value = 1ULL;
};


template<uint64_t N>
struct Fib
{
    static constexpr uint64_t Value = Fib<N - 1>::Value + Fib<N - 2>::Value;
};

template<>
struct Fib<2ULL>
{
    static constexpr uint64_t Value = 1ULL;
};
template<>
struct Fib<1ULL>
{
    static constexpr uint64_t Value = 1ULL;
};

template<int64_t base, uint64_t exp>
struct Pow
{
    static constexpr int64_t Value = base * Pow<base, exp-1>::Value;
};

template<int64_t base>
struct Pow<base, 1>
{
    static constexpr int64_t Value = base;
};

template<int64_t base>
struct Pow<base, 0>
{
    static constexpr int64_t Value = 1;
};

修改完后用法和之前完全一样。
借助C++17可以进一步简化,如下:

#include <cstdint>

template <uint64_t N>
struct Fact
{
    static constexpr uint64_t Value = []
    {
        if constexpr (N == 1)
        {
            return 1;
        }
        else
        {
            return N * Fact<N - 1>::Value;
        }
    }();
};

template <uint64_t N>
struct Fib
{
    static constexpr uint64_t Value = []
    {
        if constexpr (N < 3)
        {
            return 1;
        }
        else
        {
            return Fib<N - 1>::Value + Fib<N - 2>::Value;
        }
    }();
};

template<int64_t base, uint64_t exp>
struct Pow
{
    static constexpr int64_t Value = [] -> int64_t
    {
        if constexpr (exp == 1)
        {
            return base;
        }
        else if (exp == 0)
        {
            return 1;
        }
        else
        {
            return base * Pow<base, exp-1>::Value;
        }
    }();
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Low-Code Application Platforms(低代码应用平台)是一种开发软件应用程序的方法,它通过可视化界面和图形化编程来简化应用程序的开发过程,减少了对传统编程知识的依赖。 以下是几个具体的低代码应用平台的例子: 1. OutSystems:OutSystems是一款领先的低代码开发平台,可用于快速构建企业级Web和移动应用程序。它提供了可视化界面和大量的预构建组件,使开发人员能够快速创建功能强大且高度定制化的应用程序。 2. Mendix:Mendix是一款面向企业的低代码开发平台,可用于构建Web和移动应用程序。它具有友好的可视化界面和强大的集成能力,使开发人员能够快速集成现有系统和数据源,并创建复杂的业务应用。 3. Salesforce Lightning Platform:Salesforce Lightning Platform是一款低代码开发平台,专注于构建企业级CRM和业务应用。它提供了丰富的组件和模板,使开发人员能够快速创建定制化的销售、服务和市场营销应用。 4. Microsoft Power Apps:Microsoft Power Apps是一款低代码开发平台,可用于构建Web、移动和桌面应用程序。它与Microsoft的其他产品(如Office 365和Dynamics 365)紧密集成,使开发人员能够轻松地创建与现有Microsoft生态系统无缝集成的应用程序。 这些低代码应用平台使开发人员能够快速构建应用程序,减少了传统编码的复杂性,加快了开发速度,并提供了更高的灵活性和可定制性。它们被广泛应用于企业级应用程序开发、数字化转型和快速原型开发等方面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值