std::array
有两个模板参数:
typename T // the element type
size_t N // the size of the array
我想定义一个函数,它以 std::array 作为参数,但只适用于特定的 T,例如 char,但对于任何大小的数组都适用:
下面的代码是错误的:
void f(array<char, size_t N> x) // ???
{
cout << N;
}
int main()
{
array<char, 42> A;
f(A); // should print 42
array<int, 42> B;
f(B); // should not compile
}
如何正确地书写这个?
f(A)
函数会正确推断出 N 吗?还是需要指定为f<42>(A)
? - Andrew Tomazos