如何创建变量模板?
how to create a variable-template?
#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;
};
////////////////////////////// 我在这里创建变量模板失败。
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
用于声明type alias。
Type alias is a name that refers to a previously defined type (similar
to typedef).
Alias template is a name that refers to a family of types.
作为variable_template应该是
template<int base, int x>
constexpr int power_v = Power<base, x>::a;
#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;
};
////////////////////////////// 我在这里创建变量模板失败。
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
用于声明type alias。
Type alias is a name that refers to a previously defined type (similar to typedef).
Alias template is a name that refers to a family of types.
作为variable_template应该是
template<int base, int x>
constexpr int power_v = Power<base, x>::a;