Discussion:
Compile Error C1017 With sizeof()
(too old to reply)
Alexander J. Oss
2006-05-10 04:54:46 UTC
Permalink
Visual Studio 2005 Standard is giving me "fatal error C1017: invalid integer
constant expression" on code that compiles fine in Borland land:

#if sizeof(char)==1

The standard says:

(5.19.1) ...An integral constant-expression can involve only ... and sizeof
expressions.

and

(5.3.3.1) sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1

That last quote from the standard makes it clear that my code is pretty
useless, since by the standard I'll never have any other case. However,
this shouldn't cause a compilation error, should it?
Doug Harrison [MVP]
2006-05-10 05:12:57 UTC
Permalink
Post by Alexander J. Oss
Visual Studio 2005 Standard is giving me "fatal error C1017: invalid integer
#if sizeof(char)==1
(5.19.1) ...An integral constant-expression can involve only ... and sizeof
expressions.
and
(5.3.3.1) sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1
That last quote from the standard makes it clear that my code is pretty
useless, since by the standard I'll never have any other case. However,
this shouldn't cause a compilation error, should it?
It should, since sizeof isn't part of the preprocessor language. And you're
right, sizeof(char) is 1 by definition. In C and C++ terms, "char" and
"byte" are synonymous, and they don't necessarily imply 8 bits, though
usually they do.
--
Doug Harrison
Visual C++ MVP
Alexander J. Oss
2006-05-10 05:51:40 UTC
Permalink
Post by Doug Harrison [MVP]
It should, since sizeof isn't part of the preprocessor language. And you're
right, sizeof(char) is 1 by definition. In C and C++ terms, "char" and
"byte" are synonymous, and they don't necessarily imply 8 bits, though
usually they do.
The preprocessor language includes "constant-expression" (according to
16.1.1 and 16.1.3), and 5.19 is what defines an integral
"constant-expression", and that definition includes sizeof().

...ugh, I just found the following quote at
http://msdn2.microsoft.com/en-us/library/ew2hz0yd.aspx:

The expression cannot use sizeof or a type-cast operator.

Why is this quote present here but not in the C++ standard (that I could
find)?
Ulrich Eckhardt
2006-05-10 07:28:21 UTC
Permalink
Post by Alexander J. Oss
...ugh, I just found the following quote at
The expression cannot use sizeof or a type-cast operator.
Why is this quote present here but not in the C++ standard (that I could
find)?
Just FYI, there is a whole NG devoted to the discussion of the C++ standard,
comp.std.c++.

Uli
Alexander J. Oss
2006-05-10 14:14:22 UTC
Permalink
Post by Ulrich Eckhardt
Just FYI, there is a whole NG devoted to the discussion of the C++ standard,
comp.std.c++.
Thanks (I prefer the moderated C++ NG myself). I didn't want to bring up a
concern about a specific compiler there, though.
Alex Blekhman
2006-05-10 11:29:20 UTC
Permalink
Post by Alexander J. Oss
Post by Doug Harrison [MVP]
It should, since sizeof isn't part of the preprocessor
language. And you're
right, sizeof(char) is 1 by definition. In C and C++
terms, "char" and "byte" are synonymous, and they don't
necessarily imply 8 bits, though usually they do.
The preprocessor language includes "constant-expression"
(according to 16.1.1 and 16.1.3), and 5.19 is what defines
an integral
"constant-expression", and that definition includes
sizeof().
...ugh, I just found the following quote at
The expression cannot use sizeof or a type-cast operator.
Why is this quote present here but not in the C++
standard (that I could find)?
Because C++ Standard is terse document, which provides
shortest (most of the time) definitions of language
concepts. MSDN is broader and includes commentaries and
interpretations. Once it's in preprocessor section of the
Standard the assumption is that compiler stuff is excluded.
MSDN explicitly states this fact and enumerates limitations
for preprocessor constant expression.

`sizeof' is compile time operator, i.e. it's evaluated by
compiler. Preprocessor runs before compilation, hence
`sizeof' is unknown during this stage.
Igor Tandetnik
2006-05-10 11:36:41 UTC
Permalink
Post by Alexander J. Oss
The preprocessor language includes "constant-expression" (according to
16.1.1 and 16.1.3), and 5.19 is what defines an integral
"constant-expression", and that definition includes sizeof().
...ugh, I just found the following quote at
The expression cannot use sizeof or a type-cast operator.
Why is this quote present here but not in the C++ standard (that I
could find)?
16.1/1 The expression that controls conditional inclusion shall be an
integral constant expression except that: it shall not contain a cast;
identifiers (including those lexically identical to keywords) are
interpreted as described below;136) ...

16.1/3 ... After all replacements due to macro expansion and the defined
unary operator have been performed, all remaining identifiers and
keywords, except for true and false, are replaced with the pp-number
0...

136) Because the controlling constant expression is evaluated during
translation phase 4, all identifiers either are or are not macro names -
there simply are no keywords, enumeration constants, and so on.



That is, as far as preprocessor is concerned, sizeof(int) is the same as
0(0) , which is obviously nonsense.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Alexander J. Oss
2006-05-10 14:17:44 UTC
Permalink
Post by Igor Tandetnik
Post by Alexander J. Oss
The preprocessor language includes "constant-expression" (according to
16.1.1 and 16.1.3), and 5.19 is what defines an integral
"constant-expression", and that definition includes sizeof().
...ugh, I just found the following quote at
The expression cannot use sizeof or a type-cast operator.
Why is this quote present here but not in the C++ standard (that I
could find)?
16.1/1 The expression that controls conditional inclusion shall be an
integral constant expression except that: it shall not contain a cast;
identifiers (including those lexically identical to keywords) are
interpreted as described below;136) ...
16.1/3 ... After all replacements due to macro expansion and the defined
unary operator have been performed, all remaining identifiers and
keywords, except for true and false, are replaced with the pp-number 0...
136) Because the controlling constant expression is evaluated during
translation phase 4, all identifiers either are or are not macro names -
there simply are no keywords, enumeration constants, and so on.
That is, as far as preprocessor is concerned, sizeof(int) is the same as
0(0) , which is obviously nonsense.
And this is why one should never skip reading the footnotes. Thank you,
Igor.
Alexander Grigoriev
2006-05-10 15:50:17 UTC
Permalink
See also footnote 140 of clause 6.10.1 of ANSI C standard IEC 9899. There is
no keywords in #if expression, except for 'defined'. 'sizeof' is not
interpreted in any way.
Post by Alexander J. Oss
Post by Doug Harrison [MVP]
It should, since sizeof isn't part of the preprocessor language. And you're
right, sizeof(char) is 1 by definition. In C and C++ terms, "char" and
"byte" are synonymous, and they don't necessarily imply 8 bits, though
usually they do.
The preprocessor language includes "constant-expression" (according to
16.1.1 and 16.1.3), and 5.19 is what defines an integral
"constant-expression", and that definition includes sizeof().
...ugh, I just found the following quote at
The expression cannot use sizeof or a type-cast operator.
Why is this quote present here but not in the C++ standard (that I could
find)?
h***@40th.com
2006-05-10 17:44:30 UTC
Permalink
Post by Alexander J. Oss
#if sizeof(char)==1
Maybe you can substitute a creative use of

offsetof

which is a pre-processor thing.
--
40th Floor - Software @ http://40th.com/
iPlay : the ultimate audio player for mobiles
parametric eq, xfeed, reverb; all on a mobile
Tamas Demjen
2006-05-10 21:38:03 UTC
Permalink
Post by Alexander J. Oss
Visual Studio 2005 Standard is giving me "fatal error C1017: invalid integer
#if sizeof(char)==1
Borland has extended the standard an implemented a feature that C++
compilers are not required to do. As others have explained it, the
preprocessor doesn't know about subtle language features, such as
sizeof. Most preprocessors can only interpret literal constants. Borland
has added the sizeof expression to the preprocessor, but this is a
non-portable feature.

I realize that in some cases you want to verify the size of a structure,
like
#if sizeof(MyType) != 24
#error MyType has an invalid size
#endif

Fortunately you can do this without the preprocessor, using Boost
compile-time (static) assertions:
http://www.boost.org/doc/html/boost_staticassert.html

Tom

Loading...