From: A. McKenney on 26 Apr 2010 09:10 I'm running into a problem with my C++ compiler. I don't know whether what I am doing is illegal, or it's a compiler bug. 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! ]
From: Öö Tiib on 26 Apr 2010 22:53 On Apr 27, 3:10 am, "A. McKenney" <alan_mckenn...(a)yahoo.com> wrote: > I'm running into a problem with my C++ compiler. > I don't know whether what I am doing is illegal, > or it's a compiler bug. > > 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. 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. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jd on 26 Apr 2010 23:01 On Apr 27, 9:10 am, "A. McKenney" <alan_mckenn...(a)yahoo.com> wrote: > I'm running into a problem with my C++ compiler. > I don't know whether what I am doing is illegal, > or it's a compiler bug. > > 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. > Did you include the header that defines "size_t" #include <stddef.h> OR #include <stdlib.h> Jd -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Paul Bibbings on 26 Apr 2010 23:02 "A. McKenney" <alan_mckenney1(a)yahoo.com> writes: > > 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 -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 26 Apr 2010 23:59 On 27 Apr., 02:10, "A. McKenney" <alan_mckenn...(a)yahoo.com> wrote: > I'm running into a problem with my C++ compiler. > I don't know whether what I am doing is illegal, > or it's a compiler bug. 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 -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: C++0x support in compilers for embedded systems Next: friend function and storage specifier |