473,387 Members | 1,799 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.

Integral type for static class variables

Hi,
Why is there a restriction that only integral types can be made static
constant members of a class?

For e.g.,

class B {
private:
static const double K = 10;
};

MSVC++ 2005 gave me an error, but gcc 3.4.4 is compiling it straight.
It is mentioned in Item 2 of
Effective C++, 3rd Edition too.

Thanks
Sep 4 '08 #1
2 2443
Ranganath wrote:
Why is there a restriction that only integral types can be made static
constant members of a class?
You can make any type a static constant member of a class. You just
can't initialise it right there and then if it's not an integral type.
>
For e.g.,

class B {
private:
static const double K = 10;
};

MSVC++ 2005 gave me an error, but gcc 3.4.4 is compiling it straight.
"Straight"? Are you telling your compiler to actually compile C++ or do
you allow it to use all extensions it can possibly use?
It is mentioned in Item 2 of
Effective C++, 3rd Edition too.
I am not sure why such limitation was introduced, but I know that there
were times when _no_ object could be initialised like that, inside the
class definition. You would need to always define it outside and then
initialise in that definition. The integral static constant members
were allowed to be initialised so that they can be used to form integral
constant expressions where needed (like array sizes). Integral constant
expressions are a special case, there are no non-integral constant
expressions; all others are not truly compile-time constants. Even
double values can be different during run-time depending on the
availability of the hardware or its settings, AIUI. Since there are no
true compile-time constants of any other types except integral ones,
there is no sense to allow initialising those in the class definition,
since you're not going to be able to use them as true constants anyway.

Those are my guesses, of course.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 4 '08 #2
On Sep 4, 7:59 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Ranganath wrote:
Why is there a restriction that only integral types can be
made static constant members of a class?
You can make any type a static constant member of a class.
You just can't initialise it right there and then if it's not
an integral type.
For e.g.,
class B {
private:
static const double K = 10;
};
MSVC++ 2005 gave me an error, but gcc 3.4.4 is compiling it straight.
"Straight"? Are you telling your compiler to actually compile
C++ or do you allow it to use all extensions it can possibly
use?
Such an initialization will be allowed in C++0x; it's possible
that g++ is jumping the gun, and already supporting it. (If the
code compiled with g++ -std=c++98 -pedantic, I'd consider it a
bug. But it doesn't, at least not with g++ 4.1.0.)
It is mentioned in Item 2 of
Effective C++, 3rd Edition too.
I am not sure why such limitation was introduced, but I know
that there were times when _no_ object could be initialised
like that, inside the class definition.
Exactly. No such limitation was ever introduced. From the
beginning, only a definition could have an initializer, and the
declaration of a static data member in a class definition is
*not* a definition. (Back in the old days, implementing it as
such would have created significant implementation difficulties
for compilers.) The standard technique when one needed a
"constant integral expression" which was a member was to use an
enum (and there are no contexts in the language where you need a
"constant expression" which is not a "constant integral
expression"). The enum was felt to be a hack, and didn't have
the right type (which became very significant with introduction
of templates), so toward the end of the original standardization
procedure, the committee created a special exception to allow
initialization expressions for static const members of integral
type, provided the initialization expression was itself a
constant integral expression. (A "hack" in the language to
allow you to avoid a hack in your code:-).) As it was late in
the standardization procedure, the committee doubtlessly wanted
to make the minimum change necessary (thus avoiding any risk),
and didn't have time to analyse possible consequences of a more
extensive change. Since then, of course, we have more actual
experience, and the committee has had time to work out all
possible consequences in detail.
You would need to always define it outside and then initialise
in that definition. The integral static constant members were
allowed to be initialised so that they can be used to form
integral constant expressions where needed (like array sizes).
Integral constant expressions are a special case, there are no
non-integral constant expressions; all others are not truly
compile-time constants.
That's not quite true; there is a concept of "constant
expression" which is not an "integral constant expression".
It's more limited than an integral constant expression, however,
and is only relevant when used to initialize variables at
namespace scope (where it implies static initialization rather
than dynamic).
Even double values can be different during run-time depending
on the availability of the hardware or its settings, AIUI.
I think that's true (although I don't know a platform where it
is the case). On the other hand, given:

extern double f() ;
double d1 = f() ;
double d2 = 2.0 * 3.14159 ;

, the compiler must ensure that d2 is initialized before d1 (and
thus, that if f() uses d2, it sees 3.14159, and not 0.0).
Since there are no true compile-time constants of any other
types except integral ones, there is no sense to allow
initialising those in the class definition, since you're not
going to be able to use them as true constants anyway.
You still won't be able to use them as constants, even in C++0x,
unless you explicitly declare it constexpr. (At least, that's
how I understand the current draft.)

--
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
Sep 5 '08 #3

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

Similar topics

1
by: Greg Phillips | last post by:
Hello everyone, I have read the section on templates in The C++ Programming Language 3rd edition 3 times over the last 4 days. Still I am stumped. I'll explain a bit about what I am doing...
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: Susan Baker | last post by:
Hi, I got this error msg whilst building some classes. It is realatively asy to fix. But I just wondered, does anyone know the technical reason why one can't initialiaze a const static...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
1
by: Tomás | last post by:
When you have a constant integral in a program, like the population of a town, how do you declare it? A const variable or an enum? Up until now I've always used variables, e.g.: unsigned const...
10
by: Tomás | last post by:
When you simply want to store a number, what integral type do you use? For instance, let's say we have the following in a Poker game: struct Card { enum Suit { Hearts, Diamonds, Spades, Clubs...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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.