C ++ 11:在std :: array上定义函数< char,N> [英] C++11: defining function on std::array<char, N>

查看:292
本文介绍了C ++ 11:在std :: array上定义函数< char,N>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: array 需要两个模板参数:

typename T // the element type
size_t N // the size of the array

定义一个函数,它使用std ::数组作为参数,但只适用于特定的T,在这种情况下 char ,但对于任何大小的数组:

I want to define a function that takes a std::array as a parameter but only for a specific T, in this case char, but for any size array:

以下是格式错误的:

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
}

写这个的正确方法是什么? p>

What is the correct way to write this?

推荐答案

使用模板函数:

template<size_t N> void f(array<char, N> x) {
}

这篇关于C ++ 11:在std :: array上定义函数&lt; char,N&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆