Topic: sizeof(sizeof(x))


Author: Tom Payne <thp@roam-thp2.cs.ucr.edu>
Date: 2000/10/04
Raw View
Gary Hinger <garyh@zipperint.com> wrote:
> I believe the innermost sizeof operator has to be evaluated first. However,
> since it cannot be evaluated, the whole statement must be rejected.

> John Hickin <hickin@nortelnetworks.com> wrote in message
> news:39D1F715.DFD5FE38@nortelnetworks.com...
>> According to 5.5.3 Sizeof /2 the size of a reference or reference type
>> is the size of the referenced type. According to /1 sizeof shall not be
>> applied to an expression that has function type. Consider, now:
>>
>>   void foo();
>>   void (&rf)() = foo;
>>
>> So, if my interpretation of the above-mentioned rules is correct,
>> sizeof(rf) is the size of a function and therefore not allowed. Now
>> consider
>>
>>   size_t x = sizeof(sizeof(rf))
>>
>> One possibility is that sizeof(X) yields a value with type size_t and
>> therefore x==sizeof(size_t). Most compliers, however, reject the code.
>> Should they?

IIRC, sizeof is not required to evaluate its expression argument.

Tom Payne

---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/10/05
Raw View
John Hickin wrote:
>
> According to 5.5.3 Sizeof /2 the size of a reference or reference type
> is the size of the referenced type. According to /1 sizeof shall not be
> applied to an expression that has function type. Consider, now:
>
>   void foo();
>   void (&rf)() = foo;
>
> So, if my interpretation of the above-mentioned rules is correct,
> sizeof(rf) is the size of a function and therefore not allowed. Now
> consider
>
>   size_t x = sizeof(sizeof(rf))
>
> One possibility is that sizeof(X) yields a value with type size_t and
> therefore x==sizeof(size_t). Most compliers, however, reject the code.
> Should they?

I think yes. sizeof(rf) is an illegal expression, and therefore
it should not compile, even if it's never executed (that's
different from a legal expression causing undefined behaviour
on execution, like dereferencing a null pointer). What would
you expect in the following case?

std::size_t x = sizeof (std::sqrt(std::string()), 0);

Here again, to find out the size of the result, you don't have
to consider the left hand side of the comma operator, since
it's completely ignored. However, I guess you would be quite
surprised to get that through the compiler.

---
[ 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: wmm@fastdial.net
Date: 2000/10/05
Raw View
In article <8rg88e$3v7$1@mortar.ucr.edu>,
  thp@cs.ucr.edu wrote:
> Gary Hinger <garyh@zipperint.com> wrote:
> > I believe the innermost sizeof operator has to be evaluated first.
However,
> > since it cannot be evaluated, the whole statement must be rejected.
>
> > John Hickin <hickin@nortelnetworks.com> wrote in message
> > news:39D1F715.DFD5FE38@nortelnetworks.com...
> >> According to 5.5.3 Sizeof /2 the size of a reference or reference
type
> >> is the size of the referenced type. According to /1 sizeof shall
not be
> >> applied to an expression that has function type. Consider, now:
> >>
> >>   void foo();
> >>   void (&rf)() = foo;
> >>
> >> So, if my interpretation of the above-mentioned rules is correct,
> >> sizeof(rf) is the size of a function and therefore not allowed. Now
> >> consider
> >>
> >>   size_t x = sizeof(sizeof(rf))
> >>
> >> One possibility is that sizeof(X) yields a value with type size_t
and
> >> therefore x==sizeof(size_t). Most compliers, however, reject the
code.
> >> Should they?
>
> IIRC, sizeof is not required to evaluate its expression argument.

That's true but irrelevant.  Not evaluating the expression
means that there's no runtime processing (functions and objects
in the expression are not "used" (3.2p2) and thus need not be
defined), but that doesn't mean that checking semantic
constraints on the expression (a compile-time operation) is
suppressed.  The expression in a sizeof must be a completely
legitimate expression; it's just thrown away (including any
references to functions and objects) before generating any
code for it.

--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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: "Gary Hinger" <garyh@zipperint.com>
Date: 2000/10/04
Raw View
I believe the innermost sizeof operator has to be evaluated first. However,
since it cannot be evaluated, the whole statement must be rejected.

John Hickin <hickin@nortelnetworks.com> wrote in message
news:39D1F715.DFD5FE38@nortelnetworks.com...
> According to 5.5.3 Sizeof /2 the size of a reference or reference type
> is the size of the referenced type. According to /1 sizeof shall not be
> applied to an expression that has function type. Consider, now:
>
>   void foo();
>   void (&rf)() = foo;
>
> So, if my interpretation of the above-mentioned rules is correct,
> sizeof(rf) is the size of a function and therefore not allowed. Now
> consider
>
>   size_t x = sizeof(sizeof(rf))
>
> One possibility is that sizeof(X) yields a value with type size_t and
> therefore x==sizeof(size_t). Most compliers, however, reject the code.
> Should they?
>
>
> Regards, John.
>
> ---
> [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ 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: "John Hickin" <hickin@nortelnetworks.com>
Date: 2000/09/27
Raw View
According to 5.5.3 Sizeof /2 the size of a reference or reference type
is the size of the referenced type. According to /1 sizeof shall not be
applied to an expression that has function type. Consider, now:

  void foo();
  void (&rf)() = foo;

So, if my interpretation of the above-mentioned rules is correct,
sizeof(rf) is the size of a function and therefore not allowed. Now
consider

  size_t x = sizeof(sizeof(rf))

One possibility is that sizeof(X) yields a value with type size_t and
therefore x==sizeof(size_t). Most compliers, however, reject the code.
Should they?


Regards, John.

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]