Topic: [A correction] sizeof in integral constant expressions


Author: "Deja User" <gennaro_prota@my-deja.com>
Date: Mon, 19 Feb 2001 01:16:00 GMT
Raw View
Sorry if I was in a hurry in my previous post... I wish to make it
a bit clearer! :-)))

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)
what does it mean? We can have a const expression where sizeof
is applied to a function, provided that it is not applied directly?!


The second is: comma operators can be used inside a sizeof operand.
So consider, for instance, the following:

     char a [30];
        char b [sizeof (1, a)];

Is this illegal?

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?


Am I missing something obvious? :-)

     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: "James Kuyper Jr." <kuyper@wizard.net>
Date: Mon, 19 Feb 2001 17:04:16 GMT
Raw View
Deja User wrote:
>
> Sorry if I was in a hurry in my previous post... I wish to make it
> a bit clearer! :-)))
>
> 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)

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

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