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
set_num
member was trying to modify a member of a class. Ifset_num
is declared constexpr, then the object can't be modified, so your assignment wouldn't be valid.constexpr
member function can't modify the object, but that's like any otherconst
member function, not because the object is a "compile-time object". You can have mutable objects of literal types.constexpr
constants, it just means that objects of that type can be declared asconstexpr
constants