<html> 
	<style type="text/css">
<style type="text/css">

body { color: #000000; background-color: #FFFFFF; }
ins, ins * { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
del, del * { text-decoration:line-through; background-color:#FFA0A0 }
#hidedel:checked ~ * del, #hidedel:checked ~ * del * { display:none; visibility:hidden }

p.example { margin-left: 2em; }
pre.example { margin-left: 2em; }
div.example { margin-left: 2em; }

code.extract { background-color: #F5F6A2; }
pre.extract { margin-left: 2em; background-color: #F5F6A2;
  border: 1px solid #E1E28E; }

p.function { }
.attribute { margin-left: 2em; }
.attribute dt { float: left; font-style: italic;
  padding-right: 1ex; }
.attribute dd { margin-left: 0em; }

blockquote.std { color: #000000; background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding-left: 0.5em; padding-right: 0.5em; }
blockquote.stddel { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding-left: 0.5empadding-right: 0.5em; ; }

blockquote.stdins { text-decoration: underline;
  color: #000000; background-color: #C8FFC8;
  border: 1px solid #B3EBB3; padding: 0.5em; }

blockquote pre em { font-family: normal }

table { border: 1px solid black; border-spacing: 0px;
  margin-left: auto; margin-right: auto; }
th { text-align: left; vertical-align: top;
  padding-left: 0.8em; border: none; }
td { text-align: left; vertical-align: top;
  padding-left: 0.8em; border: none; }
  
p.grammarlhs { font-style:italic; margin-bottom: 0; margin-top: 0 }
p.grammarrhs { font-style:italic; margin-left:8em; margin-top:0; margin-bottom: 0; text-indent:-4em }
p.grammarlhs tt { font-style:normal }
p.grammarrhs tt { font-style:normal }


</style>
	</style>

<div style="text-align: right; float: right">
<p>ISO/IEC JTC1 SC22 WG21, Evolution Working Group<br>P0314R0<br>Robert Haberlach (rh633{at}cam{dot}ac.uk)<br>2016-03-28</p>
</div>

<h1>Querying the alignment of an object</h1>
This is analogous to evolution issue 98.<sup><a href="#EWG98">EWG98</a></sup> This paper also resolves core issue 1617.<sup><a href="#CWG1617">CWG1617</a></sup>

<h2>Motivation</h2>
<p>The <tt>alignas</tt> specifier is applicable to objects, affecting their alignment requirement but not their type.
It is therefore currently not possible to determine an object's actual alignment requirement. This paper proposes to permit application of <tt>alignof</tt> to objects and references.</p>

<h2>Proposed wording</h2>
Modify 3.11 [basic.align] &para;2 as indicated:
<blockquote class="std">
<ins>When applied to a type,</ins> the result of the <tt>alignof</tt> operator reflects the alignment requirement <del>of the type</del> in the complete-object case.
</blockquote>

Augment 5 [expr] &para;8 as indicated:
<blockquote class="std">
In some contexts, unevaluated operands appear (5.2.8, 5.3.3, <ins>5.3.6,</ins> 5.3.7, 7.1.6.2).
</blockquote>

Augment 5.3 [expr.unary] &para;1 as indicated:
<blockquote class="std">
<p class="grammarlhs">unary-expression:</p>
<p class="grammarrhs"><tt>alignof (</tt> type-id <tt>)</tt></p>
<p class="grammarrhs"><ins><tt>alignof </tt> postfix-expression </ins></p>
</blockquote>

Modify 5.3.6 [expr.alignof] as indicated:
<blockquote class="std">
<ol><li>An <tt>alignof</tt> expression yields the alignment requirement of its operand <del>type</del><ins>.
If the operand is a type, it shall represent</ins><del>representing</del> a complete object type, or an array thereof, or a reference to one of those types.
<ins>Otherwise, the operand shall be a possibly parenthesized <em>postfix-expression</em> that is an <em>id-expression</em> or class member access (5.2.5 [expr.ref]).
The <em>postfix-expression</em> shall designate a variable or class data member. The expression is an unevaluated operand (clause 5 [expr]). </ins></li>
<li> The result is an integral constant of type <tt>std::size_t</tt>.</li>
<li>
When <tt>alignof</tt> is applied to a reference type, the result is the alignment of the referenced type. When
<tt>alignof</tt> is applied to an array type, the result is the alignment of the element type. 
<li><ins>When the operand is a <em>postfix-expression</em> <tt>e</tt>, the result is determined as follows: 
<ul>
<li>If <tt>e</tt> designates an entity of object type, and any preceding declarations of that entity have an <em>alignment-specifier</em> (7.6.2 [dcl.align]), the result is the alignment specified by those declarations.
[<em>Note:</em> All declarations of an entity that have an <em>alignment-specifier</em> must specify equivalent alignment; see 7.6.2 [dcl.align]. <em>&mdash;end note</em>] </li>
<li>Otherwise, the result is <tt>alignof(decltype(e))</tt>. </li></ul>
[<em>Note:</em> As is the case with <tt>noexcept</tt>, subsequent applications of <tt>alignof</tt> to the same entity may yield different values. <em>&mdash; end note</em>]</ins></li>
</ol>
</blockquote>

Modify 7.6.2 [dcl.align] &para;6 as indicated (this resolves core issue 1617<sup><a href="#CWG1617">CWG1617</a></sup>):
<blockquote class="std">If <del>the defining</del> <ins>any</ins> declaration of an entity has an <em>alignment-specifier</em>,
<del>any non-defining</del> <ins>all</ins> declaration<ins>s</ins> of that entity
shall either specify equivalent alignment or have no <em>alignment-specifier</em>. <del>Conversely, if any declaration of
an entity has an <em>alignment-specifier</em>, every defining declaration of that entity shall specify an equivalent
alignment.</del>No diagnostic is required if declarations of an entity have different <em>alignment-specifier</em>s in different
translation units. [<em>Example: &hellip; &mdash;end example</em>]
<ins>[<em>Example:</em>
<pre class="example">
alignas(16) extern int i;

alignas(8) extern int i;      // error: Different alignment specified

extern int i;                 // Ok: No alignment-specifiers
</pre>
 <em>&mdash;end example</em>]</ins>
</blockquote>

Augment 14.6.2.2 [temp.dep.expr] &para;4 as indicated:
<blockquote class="std">
Expressions of the following forms are never type-dependent (because the type of the expression cannot be dependent):
<p class="grammarrhs">[&hellip;]</p>
<p class="grammarrhs"><tt>alignof (</tt> type-id <tt>)</tt></p>
<p class="grammarrhs"><ins><tt>alignof </tt> postfix-expression </ins></p>
<p class="grammarrhs">[&hellip;]</p>
</blockquote>

Augment 14.6.2.3 [temp.dep.constexpr] &para;4 as indicated:
<blockquote class="std">
Expressions of the following form are value-dependent:
<p class="grammarrhs"><em>[&hellip;]</em></p>
<p class="grammarrhs"><ins><tt>alignof </tt> postfix-expression </ins></p>
</blockquote>

<h2>Acknowledgments</h2>
The author would like to thank Daveed Vandevoorde for assistance in preparing the wording.

<h2><a id="References">References</a></h2>
<p>[<a id="EWG98">EWG98</a>] &ldquo;[tiny] Core issue 1008, Querying the alignment of an object&ldquo;: <a href="http://wg21.link/ewg98">wg21.link/ewg98</a></p>
<p>[<a id="CWG1617">CWG1617</a>] &ldquo;alignas and non-defining declarations&ldquo;: <a href="http://wg21.link/cwg1617">wg21.link/1617</a></p>
	
</html>
