ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Rep:
In-Class initialization of constant member
I know it's not possible to define a non-integer static const in C++ inside the definition of a class for obvious reasons of possible multiple instances of the same object through multiple inclusion of the same libraries. However, I would like to know if there is a way to work around that problem, because I have template that uses the type (int, float, double, quad, ...) as an argument and depending on that the constants should be of the same type (that has to do with allowing multiple precision computations). So putting these constants in a separate file (or at least outside of the class) is not really an option, because then I'd have to assign a type, which I don't want to do. Any suggestions?
To get around that issue, the project I work on has frequently put function statics inside inline member functions.
Obviously, the syntax to access the "static" is different than if it were directly a class static, but the semantics are the same. Typically, the member function in which you wrap the static does nothing other than return a reference to that static.
We have discovered several bugs in the Intel compiler (version 7 through 10) in compiling that construct and in several cases we needed to do something else because of compiler bugs when the code was correct. But those bugs were in fairly complicated examples and I expect you're using gcc, not intel as a compiler. I don't know of similar bugs in gcc.
Yes it is, and in fact it's critical for using partial-specialization as a sort of pre-processor:Run it through Comeau and through g++.
ta0kira
I do not need to run dwhitney67's code through g++ or an online checker I know it is not valid, why do you not try it yourself and see? The type has to be a constant integral or enumeration type.
I would also add that I do not think your code is valid either, it should have an out of class definition as well.
Quote:
9.4.2.2
The declaration of a static data member in its class definition is not a definition and may be of an
incomplete type other than cv-qualified void. The definition for a static data member shall appear in a
namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name
of the static data member shall be qualified by its class name using the :: operator. The initializer
expression in the definition of a static data member is in the scope of its class (3.3.6).
9.4.2.4
If a static data member is of const integral or const enumeration type, its declaration in the class
definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that
case, the member can appear in integral constant expressions. The member shall still be defined in a namespace
scope if it is used in the program and the namespace scope definition shall not contain an initializer.
Last edited by Biddle; 02-10-2009 at 04:16 AM.
Reason: added standard quotes
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.