编译器如何在具有数组的模板专业化之间进行选择?
我刚刚遇到 std::tr1::extent 模板,它让我感到困惑。我一生中从未处理过数组类型参数,所以我不明白它们是如何工作的。那么,给定 gcc type_traits
的代码,
template<typename _Tp, unsigned _Uint, std::size_t _Size>
struct extent<_Tp[_Size], _Uint>
template<typename _Tp, unsigned _Uint>
struct extent<_Tp[], _Uint>
编译器如何在这些专业化之间进行选择?我应该将什么类型传递给 extent
才能让它选择第二个?
I just came across std::tr1::extent template and it puzzled me. I never ever dealt with array type parameters in my life so I don't understand how they work. So, given the code from gcc type_traits
template<typename _Tp, unsigned _Uint, std::size_t _Size>
struct extent<_Tp[_Size], _Uint>
template<typename _Tp, unsigned _Uint>
struct extent<_Tp[], _Uint>
how does compiler chooses between those specializations? What type I should pass to extent
to get it choose the second one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
int[]
是一个不完整类型,编译器不知道它的sizeof
值。最外面的维度可能保持不完整,因为数组在大多数情况下正确运行并不重要(特别是索引仍然有效)。像int[1][]
这样的类型将不再是正确的类型。当然这可以嵌套:
int[]
is an incomplete type, the compiler doesn't know itssizeof
value. The outermost dimension may stay incomplete, because it's not important for the array to function correctly in most contexts (in particular, indexing will still work). Something likeint[1][]
wouldn't be a correct type anymore.Sure this can be nested: