Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 244
Motti Avatar asked May 05 '10 06:05

Motti


People also ask

What is the sizeof operator in C?

The sizeof operator is the most common operator in C. It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables. When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data ...

What is the use of sizeof in C++?

It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables. When sizeof () is used with the data types, it simply returns the amount of memory allocated to that data type.

How to find exact size of a type in C programming?

To find exact size of a type in C programming we use sizeof () operator. sizeof () is a special operator used to find exact size of a type in memory. The sizeof () operator returns an integer i.e. total bytes needed in memory to represent the type or value or expression. The sizeof () is much used operator by programmers.

Why does sizeof (x++) not increment x in C?

Why does sizeof (x++) not increment x in C? According to C99 Standards, the sizeof () operator only takes into account the type of the operand, which may be an expression or the name of a type (i.e int, double, float etc) and not the value obtained on evaluating the expression. Hence, the operand inside the sizeof () operator is not evaluated.


2 Answers

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.)

like image 31
GManNickG Avatar answered Oct 22 '22 16:10

GManNickG


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.)

like image 133
kennytm Avatar answered Oct 22 '22 16:10

kennytm