Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>GCC (also, LLVM C frontend) is definitely buggy. Both Comeau and MS seems to report errors though.</p> <p>The OP's snippet has at least two definite UBs and one red-herring:</p> <p>From N1570</p> <p>[UB #1] Missing <code>main</code> in hosted environment:</p> <blockquote> <p><strong>J2. Undefined Behavior</strong></p> <p>[...] A program in a hosted environment does not define a function named main using one of the specified forms (5.1.2.2.1).</p> </blockquote> <p>[UB #2] Even if we ignore the above there still remains the issue of taking the address of a <code>void</code> expression which is explicitly forbidden:</p> <blockquote> <p><strong>6.3.2.1 Lvalues, arrays, and function designators</strong> </p> <p>1 An lvalue is an expression (with an object type other than void) that potentially designates an object;64)</p> </blockquote> <p>and:</p> <blockquote> <p><strong>6.5.3.2 Address and indirection operators</strong></p> <p><strong>Constraints</strong></p> <p>1T he operand of the unary &amp; operator shall be either a function designator, the result of a [] or unary * operator, or an <strong><em>lvalue</em></strong> that designates an object that is not a bit-field and is not declared with the register storage-class specifier.</p> </blockquote> <p>[Note: emphasis on <em>lvalue</em> mine] Also, there is a section in the standard specifically on <code>void</code>:</p> <blockquote> <p><strong>6.3.2.2 void</strong></p> <p>1 The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression.</p> </blockquote> <p>A file-scope definition is a primary-expression (6.5). So, is taking the address of the object denoted by <code>foo</code>. BTW, the latter invokes UB. This is thus explicitly ruled out. What remains to be figured out is if removing the <code>extern</code> qualifier makes the above valid or not:</p> <p>In our case the, for <code>foo</code> as per §6.2.2/5:</p> <blockquote> <p>5 [...] If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.</p> </blockquote> <p>i.e. even if we left out the <code>extern</code> we'd still land in the same problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload