简体   繁体   English

什么时候使用size_t作为模板函数的参数是个坏主意?

[英]When is it a bad idea to use size_t as a parameter for a template function?

When is it a bad idea to use size_t as a parameter for a template function? 什么时候使用size_t作为模板函数的参数是个坏主意? I know that size_t is an unsigned int, but my professor mentioned in class that using size_t as a parameter for a template class is bad. 我知道size_t是一个unsigned int,但我的教授在课堂上提到使用size_t作为模板类的参数是不好的。 Any ideas why? 有什么想法吗?

There's nothing wrong with using size_t as a template parameter. 使用size_t作为模板参数没有任何问题。

There's nothing wrong with using any type as a template parameter. 使用任何类型作为模板参数没有任何问题。

The only reason I can think of is that size_t is different sizes in 32 and 64 bit. 我能想到的唯一原因是size_t在32位和64位中的大小不同。 If you were using it in some sort of serialization template function, your data serialized in 32 bit code would be incompatible with your 64 bit code. 如果您在某种序列化模板函数中使用它,则以32位代码序列化的数据将与您的64位代码不兼容。 That's more of an issue with serializing size_t and not really specific to templates. 这更像是序列化size_t的问题,而不是特定于模板的问题。 If you ask your prof, I'd be interested in hearing what he says! 如果你问你的教授,我会有兴趣听听他说的话!

Interpret this as you will, but TCC and GCC will not quietly accept size_t or unsigned int when coding for ReadFile() and many other functions in the Win32 API. 你可以解释这个,但是在编写ReadFile()和Win32 API中的许多其他函数时,TCC和GCC不会悄悄地接受size_tunsigned int It MUST be a DWORD if directed to be so. 如果被指示的话,它必须是一个DWORD No exceptions. 没有例外。 The API refs make that point very explicit, and coding otherwise soon tells you that they mean it. API引用使得这一点非常明确,否则编码很快就会告诉您它们的含义。 Do any different, and you're left with all kinds of ugly casting to be done too. 做任何不同的事情,你也会被各种丑陋的铸造所遗忘。

What I find odd is that I'd accepted advocacy of size_t from people who I thought know better than I do. 我觉得奇怪的是,我接受了那些我认为比我更了解的人们对size_t的倡导。 (Most do, amongst coders in general!) They advocated it for 'portability' reasons. (一般来说,大多数人都是编码员!)他们主张“便携性”的原因。 It is odd that they failed to consider compatibility with the Win32 API when deciding what is 'portable'. 奇怪的是,在决定什么是“便携式”时,他们没有考虑与Win32 API的兼容性。

My own view, in hindsight, is not to allow such people to lecture you on types. 事后看来,我自己的观点是不允许这样的人向你讲授类型。 As the very short answer by 'QuestionC' given above indicates, ANY type MAY be valid. 由于上面给出的'QuestionC'的非常简短的答案表明,任何类型都可能有效。 Surely the point is to DEFINE, meaning, we should know what we choose, and why. 当然,重点是定义,意思是,我们应该知道我们选择什么,为什么。 There are usually far greater reasons to determine the appropriate method for code matched to some host, such as purpose for the program. 通常有更多的理由来确定与某些主机匹配的代码的适当方法,例如程序的用途。 No type, no compiler, can be adequately prepared to make these decisions. 没有类型,没有编译器,可以做好充分的准备来做出这些决定。 That's our job. 那是我们的工作。 Context is everything, and it is foolish to think that ultimate compatibility or portability is possible merely by choosing a variable type according to advice from a person who has no idea what we're doing with it. 上下文就是一切,只要根据不知道我们正在做什么的人的建议选择变量类型,认为最终兼容性或可移植性是可能的是愚蠢的。 The more the web breaks ever more browsers and sites, the more self-evidently true that is. 网络越是打破越来越多的浏览器和网站,那就越明显无误。

Inventing yet another 'type' for 'portability' reasons makes as much sense, ultimately, as ordering 'one ring to bind them'. 为“可移植性”原因创造另一种“类型”最终有意义,因为订购“一个环来绑定它们”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM