Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c++ - sizeof taking two arguments

In C.1.3 of the C++ IS (2003. It's in the C++11 IS, too), the standard points out a difference between ISO C and C++; namely, for

char arr[100];

sizeof(0, arr) returns sizeof(char*) in C, but 100 in C++.

I can find no documentation for sizeof taking two arguments. The obvious fallback is the comma operator, but I don't think so: sizeof(arr) in C is 100; sizeof(0, arr) is sizeof(char*). Both sizeof(0, arr) and sizeof(arr) are 100 in C++.

I may be missing the whole point of the IS in this context. Can anyone help? This is similar to a question discussed back in '09, but no one referred to the IS, and I don't think the correct answer was given.


Edit: Actually, the IS is talking about the comma operator. So, for some reason (0, arr) returns a char* in C, but a char[100] in C++. Why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In C, comma operator doesn't produce an lvalue, consequently the array arr which is an lvalue decays into a pointer type which is a rvalue (in this case). So sizeof(0,arr) becomes equivalent to sizeof(char*), due to lvalue-to-rvalue conversion.

But in C++, comma operator produces an lvalue. There is no lvalue-to-rvalue conversion. So sizeof(0,arr) remains same, which is equivalent to sizeof(char[100]).

By the way, sizeof is not a function, it's an operator. So the following is completely valid C++ (and C, if you imagine printf instead of cout):

int a[100], b[200], c[300], d[400];
cout << sizeof(a,b,c,d) << endl;

Demo : http://www.ideone.com/CtEhn

You might think that I've passed 4 operands to sizeof but that is wrong. sizeof operates on the result of the comma operators. And its because of the many comma operators you see many operands.

4 operands with 3 comma operators; just like in 1+2+3+4, there're 3 operators, 4 operands.

The above is equivalent to the following (valid in C++0x):

auto & result = (a,b,c,d); //first all comma operators operate on the operands.
cout << sizeof (result) << endl; //sizeof operates on the result

Demo : http://www.ideone.com/07VNf

So it's the comma operator which makes you feel that there are many arguments. Here comma is an operator, but in function call, comma is NOT an operator, its simply argument separator.

function(a,b,c,d); //here comma acts a separator, not operator.

So sizeof(a,b,c,d) operates on the type of the result of , operators, exactly in the same way, sizeof(1+2+3+4) operates on the type of the result of + operators.

Also note that you cannot write sizeof(int, char, short), precisely because comma operator cannot operate on types. It operates on value only. I think, sizeof is the only operator in C and C++, which can operate on types as well. In C++, there is one more operator which can operates on types. Its name is typeid.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...