473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Static const non-integrals in-class initialization

Hello,

Why can't non-integral static const data members be initialized within a class?

I know how to initialize the members but I don't understand the underlying
reason to this rule. Would someone explain this to me?

Example:

class foo
{
public:
static const float f = 0.02f; // Error!
static const int i = 1; // Valid.
};
-- Miguel Guedes - X marks the spot for spammers. If you wish to get in touch
with me by email, remove the X from my address. -
Aug 25 '07 #1
5 2105
Miguel Guedes wrote:
Hello,

Why can't non-integral static const data members be initialized within a class?

I know how to initialize the members but I don't understand the underlying
reason to this rule. Would someone explain this to me?

Example:

class foo
{
public:
static const float f = 0.02f; // Error!
static const int i = 1; // Valid.
};

Because the Standard says so. I don't know if this has been changed in
the draft 0x standard.

Aug 25 '07 #2
red floyd wrote:
Miguel Guedes wrote:
>Hello,

Why can't non-integral static const data members be initialized within a class?

I know how to initialize the members but I don't understand the underlying
reason to this rule. Would someone explain this to me?

Example:

class foo
{
public:
static const float f = 0.02f; // Error!
static const int i = 1; // Valid.
};


Because the Standard says so. I don't know if this has been changed in
the draft 0x standard.
Well, yes. But I'm interested in knowing the reason why they chose to
differentiate the way non-integrals are initialized. Anyone knows?
--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me by email,
remove the X from my address. -
Aug 25 '07 #3
On 25 Srp, 13:30, Miguel Guedes <miguel.a.gue...@gmailX.comwrote:
red floyd wrote:
Miguel Guedes wrote:
Hello,
Why can't non-integral static const data members be initialized within a class?
I know how to initialize the members but I don't understand the underlying
reason to this rule. Would someone explain this to me?
Example:
class foo
{
public:
static const float f = 0.02f; // Error!
static const int i = 1; // Valid.
};
Because the Standard says so. I don't know if this has been changed in
the draft 0x standard.

Well, yes. But I'm interested in knowing the reason why they chose to
differentiate the way non-integrals are initialized. Anyone knows?

--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me by email,
remove the X from my address. -
I am only guessing. Maybe this way may be initialized only these
types, which are allowed as template parameters. But I do not know why.

Aug 25 '07 #4

Miguel Guedes <mi*************@gmailX.comwrote in message...
red floyd wrote:
Miguel Guedes wrote:
Why can't non-integral static const data members be initialized within
a class?
I know how to initialize the members but I don't understand the
underlying
reason to this rule. Would someone explain this to me?
Because the Standard says so. I don't know if this has been changed in
the draft 0x standard.

Well, yes. But I'm interested in knowing the reason why they chose to
differentiate the way non-integrals are initialized. Anyone knows?
Then you should ask in an NG that deals with those issues. Maybe
comp.std.c++.

Until *all* systems agree on the float/double format, you probably won't see
it happen.
Take a trip through <limitsto see what has to be dealt with.
Is it FPU or is it emulated? (memorex ad)<G>

--
Bob R
POVrookie
Aug 25 '07 #5
red floyd wrote:
Miguel Guedes wrote:
>Hello,

Why can't non-integral static const data members be initialized within a class?

I know how to initialize the members but I don't understand the underlying
reason to this rule. Would someone explain this to me?

Example:

class foo
{
public:
static const float f = 0.02f; // Error!
static const int i = 1; // Valid.
};


Because the Standard says so. I don't know if this has been changed in
the draft 0x standard.
The issue is that the compiler is expectedto know how to execute
const integral expressions at compile time, so the const inline int
makes sense. The compiler is not required to be able to do floating
point at compile time (it may assume that it's too hard to crosscompile).
Aug 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: A | last post by:
Hi, Consider this code: //Header File - Foo.h int i = 0; // non-static global variable class Foo{ ...
4
by: velthuijsen | last post by:
I was trying out the binary operators and did the following: class mv { private: int* Storage; int StSize; public: mv(int Size = 0);
10
by: Dave | last post by:
const static int ARRAY_SIZE = 4; Comeau online gives this warning: "ComeauTest.c", line 10: warning: storage class is not first const static int ARRAY_SIZE = 4; Why is static const...
19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
9
by: tkrogc | last post by:
I compiled the source below using the comeau compiler http://www.comeaucomputing.com/tryitout/ Why does the f funtion compile while the g function produces an lvalue error ? (I thought the two...
14
by: John Goche | last post by:
Hello, The extern keyword can be used in C and C++ to share global variables between files by declaring the variable in header file and defining it in only one of the two files. For example,...
12
by: mathieu | last post by:
Hi, Consider the following (*). Is there a way to rewrite it so that it remains convenient (N is being recomputed when array v is modified) *and* compiles :) Thanks, -Mathieu (*)
2
by: mathieu | last post by:
Hi there, I was recently help for an issue with the following code: template <typename T, unsigned int N> struct Functor { T values; };
11
by: Alan Woodland | last post by:
Hi, I'm fairly sure this is undefined behaviour, despite the fact that it compiles and 'runs' (prints "this doesn't exist") on all my platforms: #include <iostream> class foo { public:...
3
by: er | last post by:
hi all, probably a simple question: i'm used to static const T, i can also understand static const T* const, but i'm not sure what this, for example, means: template <class T> struct...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.