I'm reading C++ Templates: The Complete Guide, chapter 23. Metaprogramming. At the end, it describes
the difference in using enumeration values versus static constants in metaprogramming. Consider the following two implementations of calculating Nth power of 3:
Enumeration implementation:
// primary template to compute 3 to the Nth
template<int N>
struct Pow3 {
enum { value = 3 * Pow3<N-1>::value };
};
// full specialization to end the recursion
template<>
struct Pow3<0> {
enum { value = 1 };
};
Static constant implementation:
// primary template to compute 3 to the Nth
template<int N>
struct Pow3 {
static int const value = 3 * Pow3<N-1>::value;
};
// full specialization to end the recursion
template<>
struct Pow3<0> {
static int const value = 1;
};
Right after that it says there is a drawback to the latter version. If we got a function
void foo(int const&); and pass it the result of metaprogram: foo(Pow3<7>::value); the book says:
A compiler must pass the address of Pow3<7>::value, and that forces the
compiler to instantiate and allocate the definition for the static member. As a result,
the computation is no longer limited to a pure “compile-time” effect. Enumeration values aren’t lvalues (i.e., they don’t have an address). So, when we
pass them by reference, no static memory is used. It’s almost exactly as if you passed
the computed value as a literal.
I don't understand their explanation at all to be honest. I thought in the static constant version we evaluate the result at compile time, but the actual referencing happen in run-time because we need to pass the adress. But then I don't see much difference in the former case (enum implementation) because we should have temporary materialization there (prvalue -> xvalue conversion) and since temporary objects also have the adress my thinking process fails. Also it says "no static memory is used" which I don't understand as well since static memory should refer to allocation that happen at compile-time.
Could someone explain this whole thing in greater detail?
(enum implementation) because we should have temporary materialization there (prvalue -> xvalue conversion) and since temporary objects also have the address
they do, but the temporary xvalue only exists for the duration of the function call. It's identical to passing the function an integer literal. So, an addressable object exists temporarily, at runtime, with automatic scope.
Conversely,
... that forces the compiler to instantiate and allocate the definition for the static member. As a result, the computation is no longer limited to a pure “compile-time” effect.
This static const int is an object with static storage duration. There's no temporary materialization necessary, it's an object, it existed when your program started, and you're taking a reference to it.
If you write multiple .cpp files including the header with Pow3, and they all call foo(Pow3<3>):
the enum version will emit something like foo(27) in each translation unit, with three unrelated temporary xvalues
the static version will emit a global object equivalent to const int Pow3<3>::value = 27; and each translation unit will refer to (ie, take a reference to) the same object
I suggest looking at it this way:
There is a difference between a type, an instance of a type, and the values said type can take.
In the first case you are specifying a type (an unnamed enum) with only one possible value value (which is the compile-time constant).
There are no instantiations of the type, so no memory will be used compile-time or runtime.
Every time the code refers to Pow3<N>::value, the compiler will not create an instance of the type but will instead use the constant directly.
In the second case you are instead specifying a variable value of type int, and assigning the compile-time constant to it.
This variable exists, so memory will be used.
Every time the code refers to Pow3<N>::value, the compiler will use said variable.
Related
I ran across this seemingly odd behavior. A const at top level can be used for array size declarations, but not when the const is in a class.
Here's a compiler explorer demo for MSVC, CLANG and GCC all producing an error:
expression did not evaluate to a constant
It's not really a constant if it's not const at the top level?
There's some argument to be made because top level constants can often be stored in read-only memory, while constants that are not at top level cannot. But is this behavior correct?
struct A {
const int i{ 3 };
};
int main()
{
const int ii{ 3 };
A a;
int j[a.i]{}; // C2131: expression did not evaluate to a constant
int k[ii]{};
}
Generally, you can use (meaning perform an lvalue-to-rvalue conversion on) objects with lifetime starting outside a constant expression only if they are variables marked constexpr or their subobjects (plus some other special cases, that I don't think are important here, see [expr.const]/4 for details).
That you can use a const int variable at all in a constant expression is already a very specific exception. Essentially const-qualified integral and enumeration type variables are also usable in constant expressions if you could have added constexpr to them (meaning that their initializer expression is a constant expression).
This exception is there I guess purely for historical reasons, since it had been allowed before constexpr was introduced in C++11.
Note that all of this talks about variables and their subobjects. Non-static data members are specifically not variables and the exception doesn't apply to them. With constexpr this is more obvious by not allowing it on the declaration of a non-static data member in the first place.
The historical rule was never extended to encompass other types that could be marked constexpr, so e.g. const A a; will not help although that would actually cause a to be storable in read-only memory the same way a const int would.
If an object is none of the cases mentioned above, then an lvalue-to-rvalue conversion on it in a constant expression is not allowed, since it is assumed that the value of the object is not determined at compile-time.
Now, in theory the compiler could still do some constant folding and determine that even other objects' values are definitively known at compile-time. But I think the intention is that whether or not an expression is a constant expression should be (reasonably) well-defined independently of the implementation and so shouldn't rely on how much analysis the compiler can do.
For example
A a;
A b(a);
is also guaranteed to result in b.i == 3. How far do you want to require a compiler to go back or keep track of evaluations? You would need to make some definitive specification if you want to keep the behavior consistent between compilers. But there is already a simple method to indicate that you want the compiler to keep track of the values. You just have to add constexpr:
constexpr A a;
constexpr A b(a);
Now b.i can be used as array index (whether or not it is const and whether or not it is initialized).
With the current rules, any compiler only needs to evaluate the value of objects at compile-time when it sees a constexpr variable or a const integral/enumeration type variable. For all other variables it doesn't need to keep track of values or backtrack when it sees them used in a context which requires a constant expression.
The additional effect of constexpr implying const on the variable makes sure that its value will also never be changed in a valid program and so the compiler doesn't need worry about updating or invalidating the value after the initial computation either. And whether or not an expression is a constant expression is (mostly) implementation-dependent.
ii is a compile-time constant. Its value is known at compile-time, and cannot be changed at runtime. So, ii can be used for fixed array sizes at compile-time.
A::i is not a compile-time constant. It is a non-static instance member. Its value is not known until runtime. After an A object is constructed and its i member is initialized, the value of i cannot be changed because of the const, but the caller can initialize i with whatever value it wants, eg: A a{123};, and thus different A objects can have different i values. So, i cannot be used for fixed array sizes at compile-time. But, it can be used for dynamic array sizes at runtime, via new[], std::vector, etc.
TL;DR
Your assumption that const always implies compile time constant is incorrect. See examples at the end of this answer for more details on this.
Now the problem in using a.i as the size of an array is that in standard C++, the size of an array must be a compile time constant, but since i is a non-static data member, it requires an object to be used on. In other words, after construction of the class object nonstatic data member i gets initialized, which in turn means that a.i is not a constant expression, hence we get the mentioned error saying:
expression did not evaluate to a constant
To solve this, you can make i be a constexpr static data member, as shown below. This works because using a static data member doesn't require an object instance (and hence no this pointer).
struct A {
constexpr static int i{ 3 };
};
int main()
{
const int ii{ 3 };
A a;
int j[a.i]{}; //Correct now and works in all compilers
int k[ii]{};
}
I just don't get why a regular const works in some places but not others.
Perhaps you assuming that const implies compile time constant which is a wrong assumption. An example might help you understand this better:
int i = 10; //i is not a constant expression
const int size = i; //size is not a constant expression as the initializer is not a constant expression
//------vvvv------>error here as expected since size is not a constant expression
int arr[size]{};
On the other hand if you were to make i const as shown below, the program will work fine.
const int i = 10; //note the const added here so that now i is a constant expression
const int size = i; //size is a constant expression as the initializer is a constant expression
//------vvvv------>NO ERROR HERE as expected since size is a constant expression
int arr[size]{};
Consider code like the following, where the literal some_magic_int (e.g. 3) is given a name just to make a bit clearer what constant it represents:
void f() {
static constexpr int this_variable{some_magic_int};
do_something_with(this_variable);
}
int main() {
// ...
f();
// ...
}
I'm pretty sure constexpr has to be here: some_magic_int is literal, so it never changes, and I'm giving it a name just for clarity, not to give a mean to change it, so it should be at least const; then why not constexpr to have it at compile-time?
But what about static? Is it just unnecessary? Or is it detrimental? If so, why? And also, does it have any observable effect, when paired with constexpr in the declaration of a local variable?
As regards the question to which this is marked as duplicate of, it is about static constexpr int x [] = {}, and not static constexpr int x {}. This highlights at least one difference between that case (attributes applying to x pointer vs attributes applied to *x pointee) and my case (there's no pointer).
Furthermore, once I add constexpr to the specifier of a local variable (where it makes sense, e.g. to a int), I'm saying that variable is compile-time known. Why in the world doesn't that imply that no run-time entity is needed whatsoever?
The standard doesn’t actually ever talk about compile-time anything except to say that types are checked and templates are instantiated before execution. That means that this program must be diagnosed (not “rejected”!) even though the non-constant array length and template argument are never “used” and might plausibly be ignored by an interpreter:
template<int> void f() {}
int main(int argc,char **argv) {
if(false) {
int buf[argc]; // accepted by a common extension
f<argc>();
}
}
Beyond that, the semantics are that every evaluation is part of the ordinary execution of the program and constant folding is just an as-if optimization like any other. (After all, we can optimize argc*2*3*4 even though it contains no non-literal subexpression that could be a constant expression.) For constant expressions, this is largely unobservable because constant evaluation can’t have side effects (which also avoids interactions among constant-initialized non-block variables). It can, however, make a difference via the address of the local variable:
bool introspect(const int *p=nullptr) {
constexpr int x=0;
return p ? p==&x : introspect(&x); // always false
}
That the compiler must make such variables occupy memory if their address escapes is much of the content of the answers to the previously marked duplicate. It therefore makes sense to prefer static except perhaps when the object is large and its address doesn’t matter (e.g., for use as a template argument or via recursion).
I want to use constexpr instead of #defines wherever possible, for type safety and namespace features.
Unfortunately, I get this error: 'reinterpret_cast<SPI_TypeDef*>(1073756160)' is not a constant expression when trying.
#include <stm32f0xx.h> // #defines SPI2 as pointer to a struct of volatile unsigned ints
constexpr auto myPort = SPI2;
I'm not looking for an explanation of why reinterperet_cast cannot be used in a constexpr.
What is the modern C++ way to have a constexpr pointer to some memory mapped hardware?
One reason for this is to use these constexpr values in templated code.
constexpr code wasn't invented so that you could avoid using #defines; it's there so that you can do certain things with expressions that you couldn't otherwise. You can pass constexpr pointers or integers as template parameters, for example, since they are constant expressions.
Basically, the primary purpose of any constexpr variable or function is to be able to be used within compile-time programming. So to declare a variable to be constexpr is to say "this is a compile-time constant and it's reasonable to use it at compile-time."
A pointer whose value is an arbitrary address cannot be used at compile-time. So marking such a pointer as a compile-time value is a contradiction. So you're not allowed to do it. constexpr pointers are required to be real pointers, which are either null pointers or point to actual objects. Not to arbitrary addresses.
It's not possible. Even std::bit_cast, which can in certain cases emulate reinterpret_cast at compile-time, loses its constexpr-ness when one of the involved types is a pointer.
Like other answers already state, it is not possible for arbitrary pointers.
If it is "for type safety and namespace features [...] to some memory mapped hardware" you are asking for, why you don't just use
// assumed preconditions, since not provided in question
typedef struct {
volatile unsigned int a;
volatile unsigned int b;
} SPI_TypeDef;
SPI_TypeDef* SPI2 = (SPI_TypeDef*)0x12345678;
// actual answer
SPI_TypeDef* const myPort = SPI2;
This way your pointer myPort to some data of type struct SPI_TypeDef is const, but not the pointed-to struct.
const keyword is generally "left-assigning".
I would like to make sure that an argument given to a function points to (or references to) an object which has static storage duration class.
The solution needs to work with C++11 without compiler specific extensions.
The most similar question I found during my research is one which is limited to C language. The solutions proposed there, until now, work only with compiler specific extensions.
I thought of using non-type template parameters to restrict pointers to static storage duration like this:
/** #tparam POINTER must point to an integer with static storage duration
* (and linkage [before C++17])
* #return value pointed to by template argument */
template<int * POINTER>
int letPassStaticStorageDurationOnly()
{
return *POINTER;
}
int staticStorageInt = 42; // variable with static storage duration
int* pointerToStaticStorageInt = &staticStorageInt; // pointer to variable
int main()
{
int autoStorageInt = -42;
static int functionStaticInt = -21;
// error: because 'autoStorageInt' has no linkage
return letPassStaticStorageDurationOnly<&autoStorageInt>(); // shall fail
// error: because is a variable, not the address of a variable
return letPassStaticStorageDurationOnly<pointerToStaticStorageInt>();
// error [<C++17]: because 'functionStaticInt' has no linkage
return letPassStaticStorageDurationOnly<&functionStaticInt>();
return letPassStaticStorageDurationOnly<&staticStorageInt>(); // works
}
Unfortunately this has (at least) the following caveats:
Objects must have linkage (prior to C++17). This excludes for example function local static objects.
For each pointer a function template is instantiated. I am skeptical how much code duplication can be avoided by compiler optimization in the actual (productive) deployment. Constraints regarding the optimization level allowed to use in my case are to be defined.
How can one ensure arguments, given to functions, point to (or reference to) objects in static storage duration only? Preferably without the caveats of the proposal I outlined. Fundamentally different solutions are welcome!
class armon
{
static const int maxSize=10;
int array[maxSize];
int count=0;
int* topOfStack=array;
}
Why does maxSize need to be static for it to be used inside array?
It doesn't have to be static, but it must be a constant expression.
C++ Standard § 8.3.4 [dcl.array] (emphasis mine) :
If the constant-expression (5.19) is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero
That is, the following is also valid :
constexpr std::size_t Size() { return 10; };
struct Y
{
int array[Size()];
};
Note:
Since the compiler needs to know the size of the class, you cannot do this :
struct Y
{
const int size = 10;
int array[size];
};
Possibly making different instances of Y having different sizes.
Also note that in this context, int array[size] is not a constant expression, because it makes use of this, see the C++ standard section § 5.19 [expr.const] :
A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:
— this (5.1.1), except in a constexpr function or a constexpr constructor that is being evaluated as part of e;
(the evaluation of size is really this->size)
There are two aspects to this question
Aspect 1
C++ array is of fixed size, the size of which needs to be known during compile time. If the decision needs to be deferred during runtime, the array expression becomes ill-formed.
Aspect 2
Declaring a member variable as non-static makes it an instance variable, the value of which only exist once the object is instantiated which is done during run-time. A static variable is a class variable, the value of which can be determined during compile time.
Your particular example becomes the classic chicken-egg paradox.
class armon
{
static const int maxSize=10;
int array[maxSize];
}
In order to instantiate your class armon, you need to know its size.
In order to know its size, you need to know the size of individual members. In your particular case, you need to know the size of the array.
In order to know the size of the array, you need to know the value of the dependent variable maxSize.
In order to access the dependent variable maxSize you need to instantiate the class armon.
In order to instantiate your class armon, you need to know its size.
So, your array size dependent variable should be a constant expression, which in your particular case should be a static variable,
It doesn't have to be static, it has to be constant.
When you declare a constant inside a class you will be making a constant for each instance of the class.
Also if your maxSize is just const you would have to intialize it in the constructor initializer list because const maxSize is treated as variable whose value you can't change.
Inside a class const
keyword means "This is a constant during the hole lifetime of this object". Different objects of the same class can have different values for that constant.
But when it is a static constant there will be only one constant for all instances of the class. This means that you have to initialize constants value on the same line where you are defining it.