Topic: sizeof in integral constant expressions [Is sizeof(f()) aconstant?]


Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 22 Feb 2001 13:08:13 GMT
Raw View
Deja User wrote:
>
> On Tue, 20 Feb 2001 19:04:41 GMT, in comp.std.c++ you wrote:
>
> >Deja User wrote:
...
> 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());

That's not directly relevant. Others have pointed out elsewhere what I
myself forgot, that 'constant-expression' is a part of the grammar, and
when you parse that statement, the sizeof() expression doesn't come out
as a 'constant expression', despite the fact that it's constant and an
expression. That's because it didn't occur in a context requiring a
constant-expression. Section 5.19, and the prohibition it contains on
the use of a function, applies only to those integer constant
expressions. That's why all my examples used sizeof() to dimension an
array, which IS one of those contexts.

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

I'm talking about "function-call operators" because 5.19 does: it says
that "function-call ... operators shall not be used" in constant
expressions. Now, there is no such thing as a function-call operator in
the syntactic sense: in "f()", 'f' is the function name, and '()' are
parentheses, and syntactically the whole thing is a function-call
exression. The only sense in which function-call operators exist is
semantic, in which case the term refers to the function that will be
executed if the corresponding expression is evaluated. Using an operator
in that sense refers to the execution of the function, not the writing
down of the expression. Since the function-call expression occurs inside
the sizeof() expression, it never gets evaluated, and therefore the
corresponding function-call operator is not used. That distinction is
crucial to my argument.

I may be wrong here - this is a pretty subtle distinction I'm making. I
can't find any reference to "function call operator" anywhere except in
5.19 or in regard to operator overloading. I'm led to this
interpretation at least as much by my feeling that there's no need for
the standard to prohibit this, as I am by the actual text of the
standard. However, if

 int f();
 int array[sizeof(f())];

is in fact illegal, I'd appreciate an explanation from someone of why it
should be illegal (as opposed to an explanation of why it is illegal).

...
> > 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':

Using a function is a very different thing from using an expression that
has a function type. Using a function requires executing it. Using an
expression involve no more than writing it down, perhaps as an argument
to sizeof(), in which case it does NOT get executed.

> 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"?

It isn't used in the second case. The problem in the second case is not
that the function is used; it is that the operand has function type.
5.3.3p3 prohibits applying sizeof() directly to a function (as opposed
to applying it to a function call expression, which is what the third
example does).

---
[ 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.     ]