在 C++14 中使用 auto 实现非类型模板参数

Achieving non-type template parameters with auto in C++14

有没有一种方法可以模仿 C++14 中的 auto deduction in non type template parameters?类似于如何使用模板仿函数在 C++11 中模仿 C++14 lambda 中的无约束参数?

有点。你当然可以有非类型模板参数,但你需要指定类型。常见的成语是:

template <class T, T Value>
struct X;

但是你不能用它实例化 X<3> 之类的东西。你能做的最好的就是引入一个宏来为你提取类型:

#define DECL(expr) decltype(expr), (expr)
X<DECL(3)> x;

对于 3 来说这显然很愚蠢,但是当您想提供函数指针之类的东西作为非类型模板参数时确实有点帮助。