Why is there a sizeof… operator in C++0x?

痴心易碎 提交于 2019-12-05 18:37:23

问题


I saw that @GMan implemented a version of sizeof... for variadic templates which (as far as I can tell) is equivalent to the built in sizeof.... Doesn't this go against the second design principle: prefer libraries to language extensions?


回答1:


From Variadic Templates (Revision 3) (N2080=06-0150), page 6:

Although not strictly necessary (we can implement count without this feature), checking the length of a parameter pack is a common operation that deserves a simple syntax. Moreover, this operation may become necessary for type-checking reasons when variadic templates are combined with concepts; see Section 3.3.

(Section 3.3 talks about concepts which is irrelevant now.)




回答2:


sizeof... is just sugar, I think.

sizeof is indeed core to the language as is ..., and although a countof function could exist we already have sizeof and ... reserved so we might as well make it convenient to get the count that way.

Contrarily, if sizeof and ... weren't reserved, the idea of adding such a thing would have probably failed because new keywords tend to be frowned upon. (The less the better.)



来源:https://stackoverflow.com/questions/2771016/why-is-there-a-sizeof-operator-in-c0x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!