泛化积分模板参数

Generalize integral template parameters

有没有办法泛化一个完整的模板参数,使其支持例如intstd::size_t。这是我想到的非编译示例。有没有一种方法可以在不添加以 std::size_t 作为参数的副本的情况下实现函数 f

#include <cstddef>
#include <iostream>

template <int N>
struct foo {
    static constexpr int n = N;
    int a[N];
};

template <std::size_t N>
struct bar {
    static constexpr int n = N;
    float a[N];
};

template <template<int> typename T, int N>
void f(T<N> t) {
    std::cout << T<N>::n << " - " << N << std::endl;
}

int main() {
    bar<10> B;
    foo<20> F;

    f(B);
    f(F);
}

在 C++17 中,这是一个简单的 as

template <template <auto> typename T, auto N>
void f(T<N> ) { }

因为 c++17 您可以使用 auto 作为非类型模板参数。

template <template<auto> typename T, auto N>
...