Static constants in the ios_base class are initialized when created, which makes sense for constants. Can non-constant static member variables be initialized the same way, or is this concept only allowed for constant static members?
For non-constant static members with gnu compilers must use always define/allocate space separately from it's deceleration in the header? Is it even proper to initialize constant static members this way?
Class members can be created and initialized only for the static const (integral data type, like int, char, double etc.) members in current C++ standard. For non-static member it's not possible. However, in C++0x that facility is introduced.
Edit: For non-const static member, you can do initialization but you have to do the same in .cpp file (for non template classes). e.g.
struct A
{
static const int i = 0; // ok
static int j; // can declare in .cpp file as below
int k = 2; // error, but valid in C++0x
const int l = 3; // error, valid in C++0x
static const int m[2] = {1,2}; // error, should be an integral type
static const string n = "hi"; // error, should be an integral type
};
int A::j = 1 // declare in class body, and define outside
Because static data members must be
explicitly defined in exactly one
compilation unit.
From C++ FAQ
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12
You might want to read the whole "Constructors" section about "static data member" to clearly understand it.
http://www.parashift.com/c++-faq-lite/ctors.html
Related
consider a class with a constant member:
class foo {
public:
const static int N;
};
foo::N needs to initialized:
constexpr int foo::N = 5;
and note how using the constexpr qualifier instead of const here seems like a mistake.
but GCC, Clang and MSVC all compile just fine!
is there something in the language that allows changing qualifiers here?
is it an error overlooked by all three compilers?
Clang even allows both qualifier versions simultaneously:
constexpr int foo::N = 3;
const int foo::N = 5;
int main(){
return foo::N; //returns 3
}
what's going on?
Since obviously the value of a variable that has been declared but not defined cannot be used in a constant expression, constexpr pertains only to the definition of a variable. (Of course, if the variable is inline, perhaps because of being a static member declared constexpr, every definition must have constexpr.) It implies const (on the variable itself: constexpr char* is char *const, not const char*), so you haven’t changed the variable’s type. This is no different from
// foo.hpp
extern const int x;
// foo.cpp
constexpr int x=2;
which is perhaps less surprising.
So everything is fine! The variable’s value can be used in constant expressions only in the translation unit that contains the definition, but that’s no surprise, and could easily be construed as a feature for modularity. Clang is buggy to allow two definitions: presumably it’s trying to ignore the (deprecated, as of C++17) out-of-class definition for a constexpr static data member defined in the class.
Why can't I initialize non-const static member or static array in a class?
class A
{
static const int a = 3;
static int b = 3;
static const int c[2] = { 1, 2 };
static int d[2] = { 1, 2 };
};
int main()
{
A a;
return 0;
}
the compiler issues following errors:
g++ main.cpp
main.cpp:4:17: error: ISO C++ forbids in-class initialization of non-const static member ‘b’
main.cpp:5:26: error: a brace-enclosed initializer is not allowed here before ‘{’ token
main.cpp:5:33: error: invalid in-class initialization of static data member of non-integral type ‘const int [2]’
main.cpp:6:20: error: a brace-enclosed initializer is not allowed here before ‘{’ token
main.cpp:6:27: error: invalid in-class initialization of static data member of non-integral type ‘int [2]’
I have two questions:
Why can't I initialize static data members in class?
Why can't I initialize static arrays in class, even the const array?
Why I can't initialize static data members in class?
The C++ standard allows only static constant integral or enumeration types to be initialized inside the class. This is the reason a is allowed to be initialized while others are not.
Reference:
C++03 9.4.2 Static data members
§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.
What are integral types?
C++03 3.9.1 Fundamental types
§7
Types bool, char, wchar_t, and the signed and unsigned integer types are collectively called integral types.43) A synonym for integral type is integer type.
Footnote:
43) Therefore, enumerations (7.2) are not integral; however, enumerations can be promoted to int, unsigned int, long, or unsigned long, as specified in 4.5.
Workaround:
You could use the enum trick to initialize an array inside your class definition.
class A
{
static const int a = 3;
enum { arrsize = 2 };
static const int c[arrsize] = { 1, 2 };
};
Why does the Standard does not allow this?
Bjarne explains this aptly here:
A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.
Why are only static const integral types & enums allowed In-class Initialization?
The answer is hidden in Bjarne's quote read it closely,
"C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects."
Note that only static const integers can be treated as compile time constants. The compiler knows that the integer value will not change anytime and hence it can apply its own magic and apply optimizations, the compiler simply inlines such class members i.e, they are not stored in memory anymore, As the need of being stored in memory is removed, it gives such variables the exception to rule mentioned by Bjarne.
It is noteworthy to note here that even if static const integral values can have In-Class Initialization, taking address of such variables is not allowed. One can take the address of a static member if (and only if) it has an out-of-class definition.This further validates the reasoning above.
enums are allowed this because values of an enumerated type can be used where ints are expected.see citation above
How does this change in C++11?
C++11 relaxes the restriction to certain extent.
C++11 9.4.2 Static data members
§3
If a static data member is of const literal type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. —end note ] 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.
Also, C++11 will allow(§12.6.2.8) a non-static data member to be initialized where it is declared(in its class). This will mean much easy user semantics.
Note that these features have not yet been implemented in latest gcc 4.7, So you might still get compilation errors.
This seems a relict from the old days of simple linkers. You can use static variables in static methods as workaround:
// header.hxx
#include <vector>
class Class {
public:
static std::vector<int> & replacement_for_initialized_static_non_const_variable() {
static std::vector<int> Static {42, 0, 1900, 1998};
return Static;
}
};
int compilation_unit_a();
and
// compilation_unit_a.cxx
#include "header.hxx"
int compilation_unit_a() {
return Class::replacement_for_initialized_static_non_const_variable()[1]++;
}
and
// main.cxx
#include "header.hxx"
#include <iostream>
int main() {
std::cout
<< compilation_unit_a()
<< Class::replacement_for_initialized_static_non_const_variable()[1]++
<< compilation_unit_a()
<< Class::replacement_for_initialized_static_non_const_variable()[1]++
<< std::endl;
}
build:
g++ -std=gnu++0x -save-temps=obj -c compilation_unit_a.cxx
g++ -std=gnu++0x -o main main.cxx compilation_unit_a.o
run:
./main
The fact that this works (consistently, even if the class definition is included in different compilation units), shows that the linker today (gcc 4.9.2) is actually smart enough.
Funny: Prints 0123 on arm and 3210 on x86.
It's because there can only be one definition of A::a that all the translation units use.
If you performed static int a = 3; in a class in a header included in all a translation units then you'd get multiple definitions. Therefore, non out-of-line definition of a static is forcibly made a compiler error.
Using static inline or static const remedies this. static inline only concretises the symbol if it is used in the translation unit and ensures the linker only selects and leaves one copy if it's defined in multiple translation units due to it being in a comdat group. const at file scope makes the compiler never emit a symbol because it's always substituted immediately in the code unless extern is used, which is not permitted in a class.
One thing to note is static inline int b; is treated as a definition whereas static const int b or static const A b; are still treated as a declaration and must be defined out-of-line if you don't define it inside the class. Interestingly static constexpr A b; is treated as a definition, whereas static constexpr int b; is an error and must have an initialiser (this is because they now become definitions and like any const/constexpr definition at file scope, they require an initialiser which an int doesn't have but a class type does because it has an implicit = A() when it is a definition -- clang allows this but gcc requires you to explicitly initialise or it is an error. This is not a problem with inline instead). static const A b = A(); is not allowed and must be constexpr or inline in order to permit an initialiser for a static object with class type i.e to make a static member of class type more than a declaration. So yes in certain situations A a; is not the same as explicitly initialising A a = A(); (the former can be a declaration but if only a declaration is allowed for that type then the latter is an error. The latter can only be used on a definition. constexpr makes it a definition). If you use constexpr and specify a default constructor then the constructor will need to be constexpr
#include<iostream>
struct A
{
int b =2;
mutable int c = 3; //if this member is included in the class then const A will have a full .data symbol emitted for it on -O0 and so will B because it contains A.
static const int a = 3;
};
struct B {
A b;
static constexpr A c; //needs to be constexpr or inline and doesn't emit a symbol for A a mutable member on any optimisation level
};
const A a;
const B b;
int main()
{
std::cout << a.b << b.b.b;
return 0;
}
A static member is an outright file scope declaration extern int A::a; (which can only be made in the class and out of line definitions must refer to a static member in a class and must be definitions and cannot contain extern) whereas a non-static member is part of the complete type definition of a class and have the same rules as file scope declarations without extern. They are implicitly definitions. So int i[]; int i[5]; is a redefinition whereas static int i[]; int A::i[5]; isn't but unlike 2 externs, the compiler will still detect a duplicate member if you do static int i[]; static int i[5]; in the class.
I think it's to prevent you from mixing declarations and definitions. (Think about the problems that could occur if you include the file in multiple places.)
static variables are specific to a class . Constructors initialize attributes ESPECIALY for an instance.
Can someone tell me when are literal classes needed in C++?
I am getting a little confused from constexpr constructors, constexpr members, and I can't see what the point is. I'd like to see some practical use of it.
Also I'd want to know if a set member function needs to be constexpr, i.e.:
constexpr void set_num(int a) { num = a; }
In C++03 this object has dynamic initialization
struct Data {
int i;
int j;
};
Data init_data(); // calculate something
const Data data = init_data();
i.e. when the program starts, before main runs, the function will be called and the object gets initialized.
In C++11 the object can have constant initialization, a form of static initialization, meaning that its value is set at compile-time and it's initialized before the program begins. This is useful to avoid the static initialization order fiasco among other things. To ensure the type gets constant initialization it must be initialized by a constant expression, so must have a constexpr constructor and any functions called in the full expression must be constexpr functions.
The type Data is trivial so its implicitly-declared constructors are constexpr constructors, so to make the global data undergo constant initialization we just need to make init_data() be a constexpr function:
struct Data {
int i;
int j;
};
constexpr Data init_data(); // calculate something
constexpr Data data = init_data();
The advantage of a literal type is that such types can be used in other constant expressions i.e. in contexts that require compile-time constants. So now that we have our data object as a compile-time constant, we can use it in other constant expressions e.g. to initialize other compile-time constants:
const int i = ::data.i;
And we can use the Data type for a static data member with an in-class initializer:
struct MoreData {
static constexpr Data zerozero = Data{}; // OK, Data is a literal type
};
If Data wasn't a literal type we would have to write:
struct MoreData {
static const Data zerozero;
};
// in moredata.cc
const Data MoreData::zerozero = Data{};
And then code which only sees the header doesn't know the value of MoreData::zerozero and can't use it in compile-time optimisations.
So the advantage of the "literal type" rules is that they allow you to define new class types that can be used in constant expressions. In C++03 only very few types, such as integers, could be used in constant expressions, e.g. integer literals such as 1 or 0x23 or compile-time constants of integer type. In C++11 you can write you own types which can have moderately complicated logic in their constructors (anything that can be expressed in a constexpr function) but can still be used as a compile-time constant.
Also I'd want to know if a set member function needs to be constexpr, i.e.
A constexpr member function is a special case of a const member function, so it can't modify (non-mutable) members of the type. A setter function, which modifies the object, can't be const.
To be a literal type a class must follow some rules including having at least one constexpr constructor. That doesn't mean all objects of that type must be constexpr constants, it just means that objects of that type can be constexpr constants if they are declared as such and are initialized using one of the class' constexpr constructors. To use the Data example again, most objects in your program would not be constants:
Data d = { 0, 1 };
d.i = d.i + 5;
So if you added a setter, a function which modifies the object, then it would only make sense to use it on non-const objects of that type, and like any other functions which modifies the type it should not be
constexpr fixes a problem in C++98 when using numeric limits. Before C++11 an expression such as
std::numeric_limits<short>::max()
can not be used as integral constant, although it is almost equal to macro INT_MAX. with C++11, such an expression is declared as constexpr so that, for example,
you can use it to declare arrays or in compile-time computations (metaprogramming):
std::array<float,std::numeric_limits<short>::max()> a;
A big advantage of constexpr classes is that they may be put into .ro data which can lead to executable size reductions and performance boosts. Esp. for geometric types e.g. or similar 'simple' types this is very neat as you could also get rid of "magic" numbers. See e.g. https://www.kdab.com/kdab-contributions-to-qt-5-0-part-4/.
I've recently run into Visual C++ 2005 failing to initialize in class constants, having run into the ubiquitous error
"error C2864: ... : only static const integral data members can be initialized within a class"
from code similar to
class MyClass:
{
private:
static const double myConstant = 2.9768;
}
I've been able to figure out that non-integer types are the problem, and there are several ways to have integer constants, but I have not found a satisfactory work-around for defining constants scoped to a class. Is this type of declaration legal in later/other compilers?
In C++03, you have to initialize non-integral static constants outside the class definition:
struct Foo
{
static const double value;
};
const double Foo::value = 0.5;
In C++11, you can initialize arbitrary constexpressions from constant expressions inline:
struct Foo
{
static constexpr double value = 0.5;
};
You may or may not still have to provide a definition for the variable, depending on whether you require it elsewhere in your code (e.g. by taking its address).
Do it outside the class definition, in a source file (not a header, or you risk linker errors).
const double MyClass::myConstant = ..;
This behaviour is mandated by the C++ language standard. No legal workaround.
Take the initialization out of the header file and put this into your .cpp:
const double MyClass::myConstant = 2.9768;
My understanding is that C++ allows static const members to be defined inside a class so long as it's an integer type.
Why, then, does the following code give me a linker error?
#include <algorithm>
#include <iostream>
class test
{
public:
static const int N = 10;
};
int main()
{
std::cout << test::N << "\n";
std::min(9, test::N);
}
The error I get is:
test.cpp:(.text+0x130): undefined reference to `test::N'
collect2: ld returned 1 exit status
Interestingly, if I comment out the call to std::min, the code compiles and links just fine (even though test::N is also referenced on the previous line).
Any idea as to what's going on?
My compiler is gcc 4.4 on Linux.
My understanding is that C++ allows static const members to be defined inside a class so long as it's an integer type.
You are sort of correct. You are allowed to initialize static const integrals in the class declaration but that is not a definition.
Interestingly, if I comment out the call to std::min, the code compiles and links just fine (even though test::N is also referenced on the previous line).
Any idea as to what's going on?
std::min takes its parameters by const reference. If it took them by value you'd not have this problem but since you need a reference you also need a definition.
Here's chapter/verse:
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.
See Chu's answer for a possible workaround.
Bjarne Stroustrup's example in his C++ FAQ suggests you are correct, and only need a definition if you take the address.
class AE {
// ...
public:
static const int c6 = 7;
static const int c7 = 31;
};
const int AE::c7; // definition
int f()
{
const int* p1 = &AE::c6; // error: c6 not an lvalue
const int* p2 = &AE::c7; // ok
// ...
}
He says "You can take the address of a static member if (and only if) it has an out-of-class definition". Which suggests it would work otherwise. Maybe your min function invokes addresses somehow behind the scenes.
Another way to do this, for integer types anyway, is to define constants as enums in the class:
class test
{
public:
enum { N = 10 };
};
Not just int's. But you can't define the value in the class declaration. If you have:
class classname
{
public:
static int const N;
}
in the .h file then you must have:
int const classname::N = 10;
in the .cpp file.
Here's another way to work around the problem:
std::min(9, int(test::N));
(I think Crazy Eddie's answer correctly describes why the problem exists.)
As of C++11 you can use:
static constexpr int N = 10;
This theoretically still requires you to define the constant in a .cpp file, but as long as you don't take the address of N it is very unlikely that any compiler implementation will produce an error ;).
C++ allows static const members to be defined inside a class
Nope, 3.1 §2 says:
A declaration is a definition unless it declares a function without specifying the function's body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (7.5) and neither an initializer nor a functionbody, it declares a static data member in a class definition (9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), or a using-directive (7.3.4).