Topic: Evaluation of operand to sizeof


Author: Jacques Lolieux <j_lolieux@effix.fr>
Date: 2000/04/28
Raw View
Standard C++ says that operands to the sizeof operator are not evaluated
[5.3.3]. So far, so good. Now suppose the evaluation of the operand
would yield undefined behavior. And suppose that it is possible to
determine such undefined behavior at compile-time.

Is a compiler allowed to reject such code or should it accept it anyway?

My opinion is that a conforming compiler can refuse to compile that kind
of code, but then again...

Some samples to show the problem:

OK:     Compiles & runs
ERROR:  Compilation error
N/A:    Unsupported feature

// GNU g++ 2.95.2   OK
// MS VC++ 6 SP3    OK
// Sun CC 5         OK
// HP aCC 1.21      OK
// IBM VAC++ 5b2    OK
const int i = sizeof *(int *)0; // dereferencing the NULL pointer

// GNU g++ 2.95.2   OK
// MS VC++ 6 SP3    OK
// Sun CC 5         OK
// HP aCC 1.21      OK
// IBM VAC++ 5b2    OK
int f(...);
const int j = sizeof f(0); // call to f(...)

template<int i> char (&g())[i];
// GNU g++ 2.95.2   OK
// MS VC++ 6 SP3    N/A
// Sun CC 5         N/A
// HP aCC 1.21      OK
// IBM VAC++ 5b2    OK
const int k = sizeof g<1>(); // just OK

template<int i> char (&g())[i];
// GNU g++ 2.95.2   ERROR
// MS VC++ 6 SP3    N/A
// Sun CC 5         N/A
// HP aCC 1.21      ERROR
// IBM VAC++ 5b2    ERROR
const int l = sizeof g<0>(); // same as above but with 0 size array

--
Jacques Lolieux

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






Author: kanze@gabi-soft.de
Date: 2000/05/02
Raw View
Jacques Lolieux <j_lolieux@effix.fr> writes:

|>  Standard C++ says that operands to the sizeof operator are not
|>  evaluated [5.3.3]. So far, so good. Now suppose the evaluation of
|>  the operand would yield undefined behavior. And suppose that it is
|>  possible to determine such undefined behavior at compile-time.

|>  Is a compiler allowed to reject such code or should it accept it
|>  anyway?

No, since no undefined behavior takes place.

|>  My opinion is that a conforming compiler can refuse to compile that k=
ind
|>  of code, but then again...

|>  Some samples to show the problem:

|>  template<int i> char (&g())[i];
|>  // GNU g++ 2.95.2   ERROR
|>  // MS VC++ 6 SP3    N/A
|>  // Sun CC 5         N/A
|>  // HP aCC 1.21      ERROR
|>  // IBM VAC++ 5b2    ERROR
|>  const int l =3D sizeof g<0>(); // same as above but with 0 size array

This is not undefined behavior, but the violition of a semantic
restriction *requiring* a compile time error message.

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

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