If I have a class that contains only compile-time constants, for example,
class A {
static const int x = 1;
static const int y = 2;
static const int z = 3;
};
I believe it’s the case that, so long as the address of the constants is not taken, they can (will?) be replaced at compile time where they are used and will not take up any space in the executable (as constants that is, obviously the numbers themselves are going to have to show up). If this is the case can/will the class also be optimized out? And, will this change if something inherits from class A, but still only uses the constants themselves and does not take their addresses?
Oh, and assuming, in the non-inheritance version, that the class is not actually used itself anywhere apart from as a means to access the constants.
Thanks.
Space used
No, the static const int member will will not have any space allocated for them, as they are evaluated as compile time constants.
As for size of the class object (i.e.
sizeof(A)), this is not relevant unless you are creating instances of the class A – which you explicitly said you are not.Use namespace instead?
That said, perhaps you could use namespace instead to make your intention a bit clearer? Unless you are using it for something like template traits, it seems you are abusing class to do the job namespaces are intended for.