【问题标题】:how to create a variable-template?如何创建变量模板?
【发布时间】:2020-05-08 15:53:52
【问题描述】:
#include <iostream>
using namespace std;

template<int base, int x>
struct Power {


    static constexpr int a = base * (Power<base, x - 1>::a);

};

template<int base>
struct Power<base, 0> {


static constexpr int a = 1;

};

////////////////////////////////////////////I我在在此创建变量模板,我失败了。

template<int base, int x>       
using power_v = typename Power<base, x>::a;

/////////////////////////////

int main()
{
    constexpr int y = power_v<3, 2>;

    cout << y;
}

【问题讨论】:

  • using power_v = typename ... 创建一个类型别名,而不是一个变量。使用inline constexpr int power_v = ...

标签: c++ templates c++14 variable-templates


【解决方案1】:

using 用于声明type alias

类型别名是指先前定义的类型的名称(类似 到typedef)。

别名模板是指一系列类型的名称。

应该是variable_template

template<int base, int x>       
constexpr int power_v = Power<base, x>::a;

LIVE

【讨论】:

    猜你喜欢
    • 2023-01-24
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 2020-08-27
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多