本文介绍了使用隐式函数模板特化来检查标识符是否是数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在考虑使用函数模板来验证 标识符是否为数组类型。可以使用以下模板 用于此目的: 模板< typename T,int N> void check_array(T(&)[N]) { } 那么使用size_t而不是int呢? - [见 http://www.gotw.ca/resources/clcm.htm 有关的信息] [ comp.lang.c ++。主持。第一次海报:做到这一点! ]I''m thinking about using a function tamplate to verify that anidentifier is of an array type. May the following template beused for this purpose:template< typename T, int N >void check_array( T(&)[N] ){}And what about using size_t instead of int?--[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ]推荐答案 是的,我可能会使用''size_t'',虽然''int''几乎可以使用,如果大小小于INT_MAX。 或许将它与编译时断言相结合将会更好,并且你可以尝试加入一个不错的错误 消息进入它,但应该开箱即用...... 你为什么这么问?你遇到任何问题吗? V - 请删除大写''A'时通过电子邮件回复 我没有回复最热门的回复,请不要问Yes, I''d probably use ''size_t'', although ''int'' would work almostas well if the size is less than INT_MAX.Perhaps incorporating this with a compile-time assert would bea bit better, and you could try incorporating a decent errormessage into it, but should work right out of the box...Why do you ask? Have you run into any problem with it?V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 是的,绝对。Yes, absolutely. 确实size_t是这种方法的规范类型,因为它是 unsigned,因为每个静态可创建的数组必须有一个 尺寸可通过sizeof表示,其本身返回size_t。 来自不来梅的问候, DanielKrügler - [见 http:/ /www.gotw.ca/resources/clcm.htm 了解有关的信息] [comp.lang.c ++。moderated。第一次海报:做到这一点! ]Indeed size_t is the canonical type for this approach, because it isunsigned and because every statically creatable array must have asize expressable via sizeof, which itself returns size_t.Greetings from Bremen,Daniel Krügler--[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 是的,我可能会使用''size_t'',虽然''int''可以使用 几乎 如果大小小于INT_MAX。 或许将其与编译时断言相结合将是 a更好,Yes, I''d probably use ''size_t'', although ''int'' would workalmostas well if the size is less than INT_MAX.Perhaps incorporating this with a compile-time assert would bea bit better, 你的想法是什么?What is your idea? 是否有标准方法来合并错误消息?Is there a standard way to incorporate an error message? 不,我没有发现任何问题。我只是好奇,如果 它是可靠的,那就是模板是否应该隐式地实例化,当且仅当参数属于数组类型时才会实例化。No, I have not found any problems with it. I am just curious ifit is reliable, that is whether the template shall be implicitlyinstantiated if and only if the argument is of an array type. 这篇关于使用隐式函数模板特化来检查标识符是否是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-06 19:51