Re: in-class initialization of fundamental types
Re: in-class initialization of fundamental types
- Subject: Re: in-class initialization of fundamental types
- From: Andreas Grosam <email@hidden>
- Date: Mon, 26 Sep 2005 12:11:17 +0200
On 26.09.2005, at 04:07, Markian Hlynka wrote:
Isn't the following suppposed to be legal in c++?
class foo
{
public:
static const int foo_max_size = 100;
private:
char fooname[foo_max_size];
};
xcode (and thus gcc4) doesn't seem to allow it, but I was certain I'd read somewhere that this was now legal??
The code is legal, and conforms to the C++ standard. It's NOT a recent addition, and it is legal since 1998.
Hey everyone.
I'm totally confused. based on the responses here, I tried to use the initializer again, and this time it worked. I have no idea what I might have done before to make it not work! Oh well. Thanks to everyone who said it should work!! (more below... Though, the rest is more of a reply to Andreas.)
Other random friend's statements are definitely ********!
I'm guessing he wasn't paying careful attention at the time either!
Regarding the initializer (the part on the right side of the assignment operator) for the const class member, please read 9.4 Static Members.
The only (but restrictive) requirement for the initializer is, that it shall be an integral constant expression.
I see. Section 9.4 refers to the standard, yes? Is this easily and freely available? I haven't googled
C++ Standard" yet... because it seemed to me that it was likely to generate an awful lot of hits.
Yes, I referred to the Standard. It is not freely available, but it doesn't cost much (about 18$) for an electronic copy in PDF.
There are "public-comment versions" freely available in the web, but they differ in many details and are a bit outdated.
gcc 4 for sure conforms to this (regardless on which platform - it's the common front-end), and previous versions - i´m pretty sure - as well.
Notice, the initialization of class members within the class definition only works for "integral types", not for (all) "fundamental" types.
Er... I think I knew this. What's an example of a non-integral, fundamental type, though?
double, float
btw, do you know the neat trick with the sizeof() operator?
I don't think so. You have my attention now, so do tell!
In C++, the sizeof() operator returns an integral constant - means it can be determined at compile time.
So, if you declare a type which size you know, eg:
typedef char ar[10];
then
sizeof(ar)
returns 10 at compile time.
Then you can use it as follows:
struct X {
typedef int8 sz_10[10];
static const int Size = sizeof(sz_10);
};
This kind of "trick" is often used in compile-time assertions, template and MPL code.
Andreas
Markian
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden