Mixing extern and const

Yes, you can use them together. And yes, it should exactly match the declaration in the translation unit it’s actually declared in. Unless of course you are participating in the Underhanded C Programming Contest 🙂 The usual pattern is: file.h: extern const int a_global_var; file.c: #include “file.h” const int a_global_var = /* some const expression … Read more

How is its lifetime of a return value extended to the scope of the calling function when it is bound to a const reference in the calling function?

Normally a temporary object (such as one returned by a function call) has a lifetime that extends to the end of the “enclosing expression”. However, a temporary bound to a reference generally has it’s lifetime ‘promoted’ to the lifetime of the reference (which may or may not be the lifetime of the calling function), but … Read more

Is casting std::pair const& to std::pair const& safe?

It’s NOT portable to do so. std::pair requirements are laid out in clause 20.3. Clause 17.5.2.3 clarifies that Clauses 18 through 30 and Annex D do not specify the representation of classes, and intentionally omit specification of class members. An implementation may define static or non-static class members, or both, as needed to implement the … Read more