This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofC++17 status.
std::addressof should beconstexprSection: 20.2.11[specialized.addressof]Status:C++17Submitter: Daryle WalkerOpened: 2013-09-08Last modified: 2017-07-30
Priority:3
View all otherissues in [specialized.addressof].
View all issues withC++17 status.
Discussion:
I'm writing a function that needs to beconstexpr and I wanted to take the address of its input. I was thinking of usingstd::addressof to be safe, but it isn't currentlyconstexpr. Asample implementation couldn't beconstexpr under the C++11 rules, though.
std::addressofimplementations are not valid in constant expressions, therefore it seems more like a defect than a feature request to ask for the guarantee thatstd::addressof is aconstexpr function. It should be added that a similar requirementalready exists foroffsetof indirectly via the C99 standard as of 7.17 p3:The macros are […]
offsetof(type, member-designator)which expands to an integer constant expression that has type
size_t[…]
combined with the noted property in C++11 that:
"
offsetofis required to work as specified even if unaryoperator&is overloaded for any of the types involved"
Therefore implementations should already be able without heroic efforts to realize this functionality bysome intrinsic. The wording needs at least to ensure that for any lvalue core constant expressionethe expressionstd::addressof(e) is a core constant expression.
[2013-09 Chicago]
[2014-06-08, Daniel improves wording]
It has been ensured that the wording is in sync with the recent working paper and the usage of "any" has beenimproved to say "every" instead (the fix is similar to that applied by LWG2150(i)).
Previous resolution from Daniel [SUPERSEDED]:
Change header
<memory>synopsis, 20.2.2[memory.syn] as indicated:namespace std { […]// 26.11[specialized.algorithms], specialized algorithms: template <class T>constexpr T* addressof(T& r) noexcept; […]}Change 20.2.11[specialized.addressof] as indicated:
template <class T>constexpr T* addressof(T& r) noexcept;-1-Returns: The actual address of the object or function referenced by
-?-Remarks: For every lvalue core constant expressionr, even in the presence of anoverloadedoperator&.e(7.7[expr.const]), the expressionstd::addressof(e)is a core constant expression.
[2014-06-09, further improvements]
A new wording form is now used similar to the approach used by LWG2234(i), whichis a stricter way to impose the necessary implementation requirements.
[2015-05, Lenexa]
STL: the intent of this change is good; I think the wording is good
- I'm a bit worried about asking for a compiler hook
- if every implementer says: yes they can do it we should be good
EB: there is missing the word "a" before "subexpression" (in multiple places)
MC: the editor should do - we rely on our editors
MC: move to Review with a note stating that we wait for implementation experience first
- in favor: 13, opposed: 0, abstain: 2HB: N4430 will bring something which is addressing this issue
MC: good we didn't go to ready then
Proposed resolution:
This wording is relative to N3936.
Introduce the following new definition to the existing list in [definitions]: [Drafting note:If LWG2234(i) is accepted before this issue, the accepted wording for the new definition should be used instead—end drafting note]
constant subexpression [defns.const.subexpr]
an expression whose evaluation as a subexpression of aconditional-expressionCE (7.6.16[expr.cond]) would not preventCE from being a core constant expression (7.7[expr.const]).
Change header<memory> synopsis, 20.2.2[memory.syn] as indicated:
namespace std { […]// 26.11[specialized.algorithms], specialized algorithms: template <class T>constexpr T* addressof(T& r) noexcept; […]}Change 20.2.11[specialized.addressof] as indicated:
template <class T>constexpr T* addressof(T& r) noexcept;-1-Returns: The actual address of the object or function referenced by
-?-Remarks: An expressionr, even in the presence of anoverloadedoperator&.std::addressof(E)is a constant subexpression (3.15[defns.const.subexpr]), ifEis an lvalue constant subexpression.