Topic: sizeof in integral constant expressions [Is sizeof(f()) a
Author: "Deja User" <gennaro_prota@my-deja.com>
Date: Wed, 21 Feb 2001 19:51:31 GMT Raw View
On Tue, 20 Feb 2001 19:04:41 GMT, in comp.std.c++ you wrote:
>Deja User wrote:
>>
>> On Mon, 19 Feb 2001 17:04:16 GMT, "James Kuyper Jr." <kuyper@wizard.net> wrote:
>>
>> [snip]
>> >> There are (at least) 3 things that puzzles me about the interpretation that James Dennett proposes for 5.19.
>> >>
>> >> The first regards the following sentence:
>> >>
>> >> "except in sizeof expressions, functions ...shall not be used".
>> >>
>> >> Now, since sizeof shall not be applied directly to a function (5.3.3)
>> >
>> >That means only that the expression sizeof is applied to cannot have
>> >function type. There's lots of ways that a function can occur in a
>> >sizeof expression without violating that requirement.
>> >
>> >> what does it mean? We can have a const expression where sizeof
>> >> is applied to a function, provided that it is not applied directly?!
>> >
>> >Sure:
>> >
>> >int f(void);
>> >
>> >int i[sizeof(f)]; // Not legal, 5.3.3
>> >int j[sizeof(f())]; // Legal: ==sizeof(int)
>> >int k[sizeof(&f)]; // Legal: ==sizeof(int(*)())
>> >int l[sizeof(f()+2.3)]; // Legal: ==sizeof(double)
>> >
>> >...
>>
>> Let me recall the wording of the standard we are talking about (5.19):
>>
>> "An integral constant-expression can involve only literals (2.13), enumerators, [....]
>> In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used."
>>
>> Now, the most likely interpretation seems to be:
>>
>> - assignment, increment, decrement, function-call, or comma operators
>> shall NEVER be used.
>>
>> - functions, class objects, pointers, or references can be used only in sizeof expressions.
>>
>> According to 1. you have:
>>
>> int j[sizeof(f())]; // WRONG: function-call used
>> int k[sizeof(&f)]; // Legal: ==sizeof(int(*)())
>> int l[sizeof(f()+2.3)]; // WRONG: function-call used.
>>
>> Now, we should at least agree that the wording "is used" is quite ambiguos...(you've just said that having a function call expression as a sizeof operand means USING a function, I tend to disagree :-) ).
>
>I'm thoroughly confused by that message. I did not say anything
>explcitly about whether that counts as "USING a function".
Sorry :-(. By the way, another source of confusion is my mistake
about the subject: I replied to "[a correction]..." but I used simply "sizeof in integral constant expressions" in the subject.
But, let's focus on the issue! :-)
I referred to the fact that, basically, you are saying it is correct to say "functions....can be used is sizeof expressions", because one
can write:
sizeof (f());
> The conclusion I reached, that 'j' and 'l' are legal, implies that I thought
>that it does NOT constitute using a function-call operator; if it were
>such a use, then 5.19 would make them illegal.
Well, if you write f() you ARE using a function-call expression. (Let's
talk of 'function-call expressions', not of 'function-call operators')
So it seems that 5.19 makes them (and the array count example I provided) illegal.
Forgive me if I suspect that, but you distinguish between function
call expressions and functions,don't you? And why do you speak in
terms of function call "operator"?
Have we a common terminology? :-)
Anyhow, I'm mainly interested in knowing whether sizeof (f()) is legal or not. Consider that "The type of the function call expression
is the return type of the statically chosen function" (5.2.2).
So if you have:
int f();
then it's natural that sizeof (f()) is equivalent to sizeof (int)
and therefore a compile-time constant, isn't it?
> You seem to be
>disagreeing with this opinion that you've incorrectly attributed to me,
>yet you're also disagreeing with the conclusion I reached using the
>opposite of that opinion. If you agree that they don't constitute using
>the function, on what grounds do you label it "WRONG: function-call
>used"?
I think "using a function" is per se an ambiguos wording. In my humble opinion one should talk about 'expressions that have function type' and 'function call expressions':
int f();
sizeof (f); // sizeof operand has function type
sizeof (f()); // " " is a function call.
In the second case, what does it mean saying, for instance, a function is "used"?
That a function name is used?
Gennaro Prota.
------------------------------------------------------------
--== Sent via Deja.com ==--
http://www.deja.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]