473,410 Members | 1,953 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,410 software developers and data experts.

Why can't static const float class members be inititalized in the class?

Eg

class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};

What's the reasoning behind this seemingly arbitrary limitation?

Martin

Apr 17 '07 #1
5 3998
Martin Rennix wrote:
class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};

What's the reasoning behind this seemingly arbitrary limitation?
I would guess portability, but don't quote me on that.

Ask in comp.std.c++ -- they hold/discuss the reasoning behind any
decisions that made it to the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 17 '07 #2
Martin Rennix wrote:
Eg

class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};

What's the reasoning behind this seemingly arbitrary limitation?

Martin
The compiler is only required to know how to do integer math at compile
time.
Apr 18 '07 #3
On Apr 18, 8:41 am, Ron Natalie <r...@spamcop.netwrote:
MartinRennixwrote:
Eg
class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};
What's the reasoning behind this seemingly arbitrary limitation?
Martin

The compiler is only required to know how to do integer math at compile
time.
Is that because in the "olden days" you couldn't guarantee that a FPU
would be available, or the equivalent software floating point library?

Apr 18 '07 #4
On Apr 18, 5:20 am, Martin Rennix <martin.ren...@gmail.comwrote:
On Apr 18, 8:41 am, Ron Natalie <r...@spamcop.netwrote:
MartinRennixwrote:
Eg
class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};
What's the reasoning behind this seemingly arbitrary limitation?
The compiler is only required to know how to do integer math at compile
time.
Also, the fact that there is no need for it. It's really a
hack, which was added for integers is because certain uses
require an integral constant expression.
Is that because in the "olden days" you couldn't guarantee that a FPU
would be available, or the equivalent software floating point library?
It's because even today, cross-compilers exist, and floating
point results depend on the actual floating point being used.
Something like 2. * 3.14 will have different values on different
machines. And the standard doesn't impose emulation of the
target floating point hardware.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 18 '07 #5
Martin Rennix wrote:
>
Is that because in the "olden days" you couldn't guarantee that a FPU
would be available, or the equivalent software floating point library?
I think it was primarily there for crosscompilers. Integer math is
pretty easy to do universally, floating point is a bit more involved.
Not everybody uses the IEEE fp math.
Apr 18 '07 #6

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

Similar topics

5
by: Thomas Matthews | last post by:
Hi, I have three classes: Category Author Publisher Each of these classes stores its information into a database table of <ID, text>. (They are fields that have a foreign key.) There is...
4
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside...
2
by: trying_to_learn | last post by:
while seeing an example i was surprised to see that a const member function is allowed to change a static data member as shown below. I am trying to reason why.... and my guess is that static data...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
2
by: Daniel Switkin | last post by:
Hi there, I'm trying to do the following: class tLimits { static const int kIntMin = 0; // fine static const float kFloatMin = 0.0f; // breaks }; and I get this message:
2
by: msaladin | last post by:
Hi all, I spent today with finding an error, I found it, though I don't no why. I have a class with static constants, like this: class EXPORT_API BusConstants { public: static const...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.