My understanding is that you can initialize
a static const integer in the class declaration.
But my compiler (Sun studio11) doesn't allow it
for size_t, which I always assumed was an integer
type. Is initialization in the class declaration
limited to only certain integral types?
The following code compiles OK
if I comment out the #define:
#define USE_SIZE_T
class A
{
public:
#ifdef USE_SIZE_T
static const size_t s = 10;
#else
static const int s = 10;
#endif
char array[s];
};
#ifdef USE_SIZE_T
const size_t A::s;
#else
const int A::s;
#endif
If I leave it in, it seems to think that size_t is
a variable name.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
size_t is yes synonym for implementation defined unsigned integral
type, so it should work. However i believe that a program is ill-
formed if you use identifier size_t without including <cstddef> that
introduces that name. Problem may be is there since your code example
does not show that you include it.
Did you include the header that defines "size_t"
#include <stddef.h> OR
#include <stdlib.h>
Jd
>
> The following code compiles OK
> if I comment out the #define:
>
> #define USE_SIZE_T
> class A
> {
> public:
> #ifdef USE_SIZE_T
> static const size_t s = 10;
> #else
> static const int s = 10;
> #endif
> char array[s];
> };
>
>
> #ifdef USE_SIZE_T
> const size_t A::s;
> #else
> const int A::s;
> #endif
>
>
> If I leave it in, it seems to think that size_t is
> a variable name.
Have you made size_t available to your program with #include <cstddef>,
or some other include that brings it in?
Regards
Paul Bibbings
This is a compiler bug, see below.
> My understanding is that you can initialize
> a static const integer in the class declaration.
> But my compiler (Sun studio11) doesn't allow it
> for size_t, which I always assumed was an integer
> type. Is initialization in the class declaration
> limited to only certain integral types?
Referring to the C++ standard 14882:2003(E) the answer
is clear, [class.static.data]/4 says:
"If a static data member is of const integral or const
enumeration type, its declaration in the class definition
can specify a constant-initializer which shall be an
integral constant expression (5.19)."
size_t is defined in <stddef.h> and in <cstddef> both
being required to be the same type. I have only a recent
C99 standard available, which says that size_t is an
"unsigned integer type" (7.17).
Finally, as of [basic.fundamental]/7 of above quoted C++
standard we have:
"Types bool, char, wchar_t, and the signed and unsigned
integer types are collectively called integral types. A
synonym for integral type is integer type.[..]"
Thus, I see no way of an alternative reading that would
exclude std::size_t from the list of valid types that
allow for in-class initialization syntax.
HTH & Greetings from Bremen,
Daniel Krügler
I do not. This snippet had no #include's at all.
I tried including <cstddef>, but that is not enough: you need
a using declaration, too:
#include <cstddef>
using std::size_t;
I believe you, that it's required by the standard,
but it's not a very well-publicized fact.
I have been programming in C++ for over 10 years,
and this is the first time I have ever heard this
or ever had to worry about defining size_t.
I had assumed that size_t was a new built-in type, the
way I've seen it used in the standard texts.
If it is mentioned in Josuttis or Stroustrup
that you need <cstddef>, I missed it.
I can only assume that all the C++ headers and
quite a few C headers on all the implementations
I use already define size_t, and this was
the first time I hadn't included one.
FWIW, a quick cruise through /usr/include on
Solaris shows size_t being typedef'ed in
quite a few files.
Same for the various flavors of C++ headers.
Why should you do that? Either include <stddef.h> and
use size_t or include <cstddef> and use std::size_t.
> I believe you, that it's required by the standard,
> but it's not a very well-publicized fact.
>
> I have been programming in C++ for over 10 years,
> and this is the first time I have ever heard this
> or ever had to worry about defining size_t.
> I had assumed that size_t was a new built-in type, the
> way I've seen it used in the standard texts.
If you explicitly name size_t you need to include
<stddef.h> or some of the other dedicated includes
that promise so, like <stdio.h>. Only implicit
usage is possible without such an include like
sizeof(char);
> If it is mentioned in Josuttis or Stroustrup
> that you need <cstddef>, I missed it.
The C standard already requires that, see 6.5.3.4/4:
"The value of the result is implementation-defined,
and its type (an unsigned integer type) is size_t,
defined in <stddef.h> (and other headers)."
or 7.17/2 (Both quotes from the recent C99 standard)
> I can only assume that all the C++ headers and
> quite a few C headers on all the implementations
> I use already define size_t, and this was
> the first time I hadn't included one.
Yes, this is rather probable.
HTH & Greetings from Bremen
Daniel Krügler