有沒有辦法概括一個完整的模板引數,以便它支持例如int和std::size_t. 這是我想到的非編譯示例。有沒有辦法在不添加作為引數f的副本的情況下實作該功能?std::size_t
#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);
}
uj5u.com熱心網友回復:
由于c 17您可以使用auto非型別模板引數。
template <template<auto> typename T, auto N>
...
uj5u.com熱心網友回復:
在 C 17 中,這是一個簡單的 as
template <template <auto> typename T, auto N>
void f(T<N> ) { }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417538.html
標籤:
