【发布时间】: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