c++ - 如何创建一个变量模板?

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

#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;

LIVE

关于c++ - 如何创建一个变量模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61683016/

相关文章:

c++ - 继承类模板有好处吗?

c++ - 无法使用 `std::make_shared` 分配指针

c++ - Visual Studio 在白色窗口而不是控制台中运行 C++

c++ - 返回变量为 void (*)(void)

c++ - 默认参数和参数包之间的交互(GCC 和 clang 不同意)

c++ - C/C++ : A (*eval(A (*function)(B), B b))(){ ... } 可能吗? (可能是 C++11 之前的版本)

c++ - 如何使用 Windows 程序在 C++ 中获取控制台输出?

c++ - 将COFF文件的COMDAT符号解释为人类可读的功能签名

c++ - 解析除关键字之外的标识符

c++ - 使 std::function 需要复制构造函数的基本原理