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

Initializing static const class members

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:

error C2864: 'kFloatMin' : only const static integral
data members can be initialized inside a class or struct

We've been using this technique fine under gcc, both 2.9
and 3.3. Is there any reason VC++ .NET 2003 can't handle
a floating point static const initialized within the
header? Pushing the definition into the .cpp file will
cost a memory access which I don't want. Since I'm
passing classes like this to templates there's a big
rippling effect in this limitation. I'd really like to
see this fixed in a future release.

Thanks,
Daniel Switkin
Nov 16 '05 #1
2 7471
Hi Daniel,
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:

error C2864: 'kFloatMin' : only const static integral
data members can be initialized inside a class or struct

We've been using this technique fine under gcc, both 2.9
and 3.3. Is there any reason VC++ .NET 2003 can't handle
a floating point static const initialized within the
header? Pushing the definition into the .cpp file will
cost a memory access which I don't want. Since I'm
passing classes like this to templates there's a big
rippling effect in this limitation. I'd really like to
see this fixed in a future release.

As Jeff pointed out, technically speaking, VC++ is perfectly conformant in
this regard, since the ISO spec defines constant initializers only for
static members of const integral or const enumeration types (see 9.4.2 sub 4
for the details).

--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #2
>As Jeff pointed out, technically speaking, VC++ is
perfectly conformant in
this regard, since the ISO spec defines constant initializers only forstatic members of const integral or const enumeration types (see 9.4.2 sub 4for the details).


Thanks for the response, both of you. I suppose this is
one of those nice extensions I've gotten used to.

There are real optimizations to be had here, though. For
example, if I have a float constant of 1.0f in the
header, this lets gcc move 0x3f800000 directly rather
than doing a memory access, which could miss the cache.
I'm currently using these float constants for default
arguments to inline functions (i.e. the alpha channel of
a color class), among other uses.

So it may not be strictly required by the language, but
it would be nice to have (especially when using
templates - this is causing some integer code to slow
down as a result).

Cheers,
Daniel Switkin
Nov 16 '05 #3

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

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
2
by: Neno | last post by:
Hi, I have a linkage error that has something to do with the use of a static member array and I can't understand how to solve the problem. Can someone help me please? In detail: A class Month...
4
by: trying_to_learn | last post by:
I'm learning consts in C++ and the book says that u have to initialize non-static consts inside the constructor initializer list, however "const string* stack" isn't initialized in the constructor...
5
by: john smith | last post by:
Hi, I am trying to initialize a float in a class. For example: class ABC{ public:
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
2
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
10
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static...
9
by: Christopher | last post by:
Code compiles, but the string evaluates to NULL in debugger. Using gcc 3.1.1. Did I not initialize the string properly or is this a compiler bug? // some.h #include <string> namespace ns {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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?
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...

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.