Quick Q: Why are static addresses constexpr?

Quick A: Because it is defined at compile time in a special section.

Recently on SO:

A little confused about constexpr functions

If 'x' is declared static, there are no errors. Why?

This is because there is always exactly one x in the program. It has an address (somewhere in the .data segment under normal conditions).

Confusingly, both static and extern keywords specify the storage duration as static (they differ in linkage)

How is it possible to get a variable address during the compilation process? Aren't they allocated at run-time?
Variables with automatic, dynamic or thread storage durations are allocated at runtime. Static duration variables are allocated by the compiler. (The linker and OS can change the location, but they know how to fix all the references when they do)

 

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.