Topic: sizeof in integral constant expressions
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 20 Feb 2001 19:04:41 GMT Raw View
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". 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. 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 tried parsing your message as making a distinction between
"using a function" and "using a function-call operator", but I still
couldn't make it make sense. Did you accidentally leave out a word or
two that reverses the meaning?
---
[ 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. ]
Author: James Dennett <jdennett@acm.org>
Date: Sat, 17 Feb 2001 18:45:53 GMT Raw View
Deja User wrote:
>=20
> Hello everybody,
>=20
> =A75.19, par.1 of the standard says:
>=20
> "An integral constant-expression can involve only literals (2.13), enum=
erators, [...long list (and type conversion stuff) snipped]
> In particular, except in sizeof expressions, functions, class objects, =
pointers, or references shall not be used, and assignment, increment, dec=
rement, function-call, or comma operators shall not be used."
>=20
> This wording isn't very clear to me in several places (for instance, if=
the expression contains class objects only as sizeof operands, is it cor=
rect to say it doesn't "involve" class objects?)
It says that
( i) the following may not appear anywhere in a constant expresion --
assignment,
increment, decrement, function-call and comma operator.
(ii) if the following appear in a constant expression, it must be in the
argument of
sizeof.
That is not the complete rules, just a consequence of the complete rules
-- hence
the wording "in particular" before these Statements.
To my mind, there's no other way to interpret the quoted sentence,
though I agree
that the wording could be clearer.
-- James Dennett <jdennett@acm.org>
---
[ 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. ]
Author: "Deja User" <gennaro_prota@my-deja.com>
Date: Sun, 18 Feb 2001 17:55:55 GMT Raw View
On Sat, 17 Feb 2001 18:45:53 GMT, James Dennett <jdennett@acm.org> wrote:
>It says that
>( i) the following may not appear anywhere in a constant expresion --
>assignment,
> increment, decrement, function-call and comma operator.
>(ii) if the following appear in a constant expression, it must be in the
>argument of
> sizeof.
>
Well, if this is the way we should interpret the whole, there are two things that are ....
The first is:
1. "except in sizeof expressions, functions ...shall not be used", but, hey, sizeof shall not be applied directly to a function. (5.3.3)
So, what does it mean? We can apply it in a const expression but not directly? Quite strange! :-)))
The second is:
2. comma operators shall not be used at all. For this see:
http://www.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#188
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. ]
Author: "Deja User" <gennaro_prota@my-deja.com>
Date: Mon, 19 Feb 2001 21:15:08 GMT Raw View
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 :-) ).
>> Last, but NOT least: the tecnique to get the count of an array I described in my first post is used in the boost library! Did my
>> teachers make a mistake?
>
>My newsreader won't let me trace back to your original post, so I don't
>know what technique you're referring to. However, the traditional method
>that works even in C and is quite legal in C++ is:
>
> #define ELEMENTS(array) (sizeof (array)/sizeof (array[0]))
>
The tecnique I was referring is the following:
template <typename T, std::size_t sz>
char (&sizer(T (&)[sz]))[sz];
int a [200];
double x [sizeof(sizer(a))];
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. ]